无法从Firebase作为Android中的列表读取数据

时间:2018-07-25 15:23:22

标签: firebase firebase-realtime-database

我阅读了许多文章,但仍然仍然存在问题,同时从firebase中获取数据作为列表。你能帮我这个忙吗?

  

错误:com.google.firebase.database.DatabaseException:无法转换   类型为java.lang.String的对象,类型为SubCategoryLoad Object

Firebase database image

enter image description here


->从Firebase加载数据:

mSubCategoryDatabaseRef = FirebaseDatabase.getInstance().getReference("user_post_add_database_ref").child("yeswanth599").child(mCategoryNameReceive);


    mSubCategoryDatabaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                GenericTypeIndicator<Map<String,SubCategoryLoad>> genericTypeIndicator=new GenericTypeIndicator<Map<String,SubCategoryLoad>>(){};
            (-->Error Showing in this Line)   Map<String,SubCategoryLoad> map=(Map<String, SubCategoryLoad>)postSnapshot.getValue(genericTypeIndicator);
                assert map != null;
                mSubCategoryLoad=new ArrayList<>(map.values());

            }

            mSubCategoryAdapter = new SubCategoryDisplayAdapter(getContext(), mSubCategoryLoad);

            mSubCategoryRecyclerView.setAdapter(mSubCategoryAdapter);
            mSubCategoryProgressCircle.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Toast.makeText(getContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
            mSubCategoryProgressCircle.setVisibility(View.INVISIBLE);
        }
    });

-> SubCategoryLoad.C​​lass

公共类SubCategoryLoad {

private String mUserPostAddress;

private String mUserPostBusinessEndTime;

private String mUserPostBusinessSelectedCity;

private String mUserPostBusinessSelectedCountry;

private String mUserPostBusinessStartTime;

private String mUserPostCategory;

private String mUserPostEmail;

private List<UserPostAdsImages> mUserPostImages;

private String mUserPostName;

private String mUserPostPhonenumber;

private List<UserPostAdsLanguages> mUserPostSupportingLanguage;

private String mUserPostWebsite;


public SubCategoryLoad() {
    //empty constructor needed
}

public SubCategoryLoad(String userPostAddress, String userPostBusinessEndTime,
                       String userPostBusinessSelectedCity, String userPostBusinessSelectedCountry,
                       String userPostBusinessStartTime, String userPostCategory,
                       String userPostEmail, List<UserPostAdsImages> userPostImages,
                       String userPostName, String userPostPhonenumber,
                       List<UserPostAdsLanguages> userPostSupportingLanguage, String userPostWebsite
) {

    mUserPostAddress = userPostAddress;
    mUserPostBusinessEndTime = userPostBusinessEndTime;
    mUserPostBusinessSelectedCity = userPostBusinessSelectedCity;
    mUserPostBusinessSelectedCountry = userPostBusinessSelectedCountry;
    mUserPostBusinessStartTime = userPostBusinessStartTime;
    mUserPostCategory = userPostCategory;
    mUserPostEmail = userPostEmail;
    mUserPostImages = userPostImages;
    mUserPostName = userPostName;
    mUserPostPhonenumber = userPostPhonenumber;
    mUserPostSupportingLanguage = userPostSupportingLanguage;
    mUserPostWebsite = userPostWebsite;


}


public void setUserPostAddress(String userPostAddress) {
    this.mUserPostAddress = userPostAddress;
}

public void setUserPostBusinessEndTime(String userPostBusinessEndTime) {
    this.mUserPostBusinessEndTime = userPostBusinessEndTime;
}

public void setUserPostBusinessSelectedCity(String userPostBusinessSelectedCity) {
    this.mUserPostBusinessSelectedCity = userPostBusinessSelectedCity;
}

public void setUserPostBusinessSelectedCountry(String userPostBusinessSelectedCountry) {
    this.mUserPostBusinessSelectedCountry = userPostBusinessSelectedCountry;
}

public void setUserPostBusinessStartTime(String userPostBusinessStartTime) {
    this.mUserPostBusinessStartTime = userPostBusinessStartTime;
}

public void setUserPostCategory(String userPostCategory) {
    this.mUserPostCategory = userPostCategory;
}

public void setUserPostEmail(String userPostEmail) {
    this.mUserPostEmail = userPostEmail;
}

public void setUserPostImages(List<UserPostAdsImages> userPostImages) {
    this.mUserPostImages = userPostImages;
}

public void setUserPostName(String userPostName) {
    this.mUserPostName = userPostName;
}

public void setUserPostPhonenumber(String userPostPhonenumber) {
    this.mUserPostPhonenumber = userPostPhonenumber;
}

public void setUserPostSupportingLanguage(List<UserPostAdsLanguages> userPostSupportingLanguage) {
    this.mUserPostSupportingLanguage = userPostSupportingLanguage;
}

public void setUserPostWebsite(String userPostWebsite) {
    this.mUserPostWebsite = userPostWebsite;
}

public String getUserPostAddress() {
    return mUserPostAddress;
}

public String getUserPostBusinessEndTime() {
    return mUserPostBusinessEndTime;
}

public String getUserPostBusinessSelectedCity() {
    return mUserPostBusinessSelectedCity;
}

public String getUserPostBusinessSelectedCountry() {
    return mUserPostBusinessSelectedCountry;
}

public String getUserPostBusinessStartTime() {
    return mUserPostBusinessStartTime;
}

public String getUserPostCategory() {
    return mUserPostCategory;
}

public String getUserPostEmail() {
    return mUserPostEmail;
}

public List<UserPostAdsImages> getUserPostImages() {
    return mUserPostImages;
}

public String getUserPostName() {
    return mUserPostName;
}

public String getUserPostPhonenumber() {
    return mUserPostPhonenumber;
}

public List<UserPostAdsLanguages> getUserPostSupportingLanguage() {
    return mUserPostSupportingLanguage;
}

public String getUserPostWebsite() {
    return mUserPostWebsite;
}

}


-> SubCategoryDisplayAdapter.class

  SubCategoryLoad subCategoryLoadCurrent = mSubCategoryLoad.get(position);
  holder.mSubCategoryAdsTitle.setText(subCategoryLoadCurrent.getUserPostName());
    holder.mSubCategoryAdsSupportingLanguagesList.setText("English,Japanese");

    //Log.i(TAG, "message:"+subCategoryLoadCurrent.getUserPostImages().get(0).getUserPostImageList());

    Glide.with(mContext)
            .load(subCategoryLoadCurrent.getUserPostImages().get(0).getUserPostImageList())
            .into(holder.mSubCategoryAdsImage);

感谢与问候, 是的。

1 个答案:

答案 0 :(得分:3)

我过去在解析时遇到同样的问题,问题是我没有使用自定义对象列表,而我应该使用这样的Map

scanf(" %c", &num);

Firebase中的该字段包含一个键和一个状态(真或假)。

您还可以查看文档中的代码,以了解它们如何解析这些对象。

https://firebase.google.com/docs/database/android/read-and-write?authuser=0#read_data_once