如何通过检索Firebase数据来填充列表适配器?

时间:2019-01-14 05:07:07

标签: android list firebase-realtime-database adapter listadapter

我正在尝试查询Firebase数据库,以将子值存储在列表适配器内的持有人中。在哪里存储数据快照值以将其用于init持有人?

从firebase检索数据的查询不起作用。

    DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
    Query query = reference
            .child(mContext.getString(R.string.dbname_user_account_settings))
            .orderByChild(mContext.getString(R.string.field_user_id))
            .equalTo(getItem(position).getUser_id());
    Log.d(TAG, "getting the data");
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.d(TAG, "onDataChange: getting the data 2");
            for ( DataSnapshot singleSnapshot :  dataSnapshot.getChildren()){
                Log.d(TAG, "onDataChange: getting the data 3");

                holder.username.setText(
                      singleSnapshot.getValue(UserAccountSettings.class).getUsername());


                ImageLoader imageLoader = ImageLoader.getInstance();

                imageLoader.displayImage(
                        singleSnapshot.getValue(UserAccountSettings.class).getProfile_photo(),
                        holder.profileImage);
            }

        }

UserAccountSettings.class:

  

公共类UserAccountSettings实现Parcelable {

private String description;
private String display_name;
private String profile_photo;
private String username;
private String website;

public UserAccountSettings(String description, String display_name, String profile_photo, String username,
                           String website) {
    this.description = description;
    this.display_name = display_name;
    this.profile_photo = profile_photo;
    this.username = username;
    this.website = website;
}

public UserAccountSettings() {

}

protected UserAccountSettings(Parcel in) {
    description = in.readString();
    display_name = in.readString();
    profile_photo = in.readString();
    username = in.readString();
    website = in.readString();
}

public static final Creator<UserAccountSettings> CREATOR = new Creator<UserAccountSettings>() {
    @Override
    public UserAccountSettings createFromParcel(Parcel in) {
        return new UserAccountSettings(in);
    }

    @Override
    public UserAccountSettings[] newArray(int size) {
        return new UserAccountSettings[size];
    }
};

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getDisplay_name() {
    return display_name;
}

public void setDisplay_name(String display_name) {
    this.display_name = display_name;
}

public String getProfile_photo() {
    return profile_photo;
}

public void setProfile_photo(String profile_photo) {
    this.profile_photo = profile_photo;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getWebsite() {
    return website;
}

public void setWebsite(String website) {
    this.website = website;
}


@Override
public String toString() {
    return "UserAccountSettings{" +
            "description='" + description + '\'' +
            ", display_name='" + display_name + '\'' +
            ", profile_photo='" + profile_photo + '\'' +
            ", username='" + username + '\'' +
            ", website='" + website + '\'' +
            '}';
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(description);
    dest.writeString(display_name);
    dest.writeString(profile_photo);
    dest.writeString(username);
    dest.writeString(website);
} }

错误:- com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为com.example.ekagra.project.Models.UserAccountSettings

我希望在holder.username中检索文本,在holder.profile图像中检索照片URL,但是for循环似乎甚至无法正常工作,因为“获取数据3”日志未显示在监视器中。

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

记录快照中您得到的结果 Log.d(TAG, "onDataChange: getting the data 3" + singleSnapshot);

然后告诉我你得到了什么。 您还可以添加数据库图片以供参考

答案 1 :(得分:0)

我通过删除for循环对其进行了修复。