如何读取 firestore 子集合并将其传递给 FirestoreRecyclerOptions

2023-12-09

enter image description hereI have firestore database with root products and every products has collection 'comments' so i stored in it all users comments about this product , but when query on this comments sub-collection i get null values or zero snapshots from firestore

   private void getCommentObject(){



    query = FirebaseFirestore.getInstance()
            .collection("products").document(docID).collection("comments");

    FirestoreRecyclerOptions<CommentModel> options = new FirestoreRecyclerOptions.Builder<CommentModel>()
            .setQuery(query, CommentModel.class)
            .build();


    adapter = new FirestoreRecyclerAdapter<CommentModel, commentHolder>(options) {
        @NonNull
        @Override
        public commentHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.comment_item_layout, parent, false);

            return new commentHolder(view);
        }

        @Override
        protected void onBindViewHolder(@NonNull commentHolder commentHolder, int position, @NonNull CommentModel commentModel) {


            commentHolder.full_comment.setText(String.valueOf(commentModel.getComment()));
            commentHolder.comment_date.setText(String.valueOf(commentModel.getCommentDate()));
            commentHolder.comment_user.setText(String.valueOf(commentModel.getCommentUser()));


            Glide.with(getApplicationContext())
                    .load(commentModel.getProfilePic())
                    .into(commentHolder.userProfileImg);

        };

    };



    rv.setAdapter(adapter);
    adapter.notifyDataSetChanged();

}

这是我的评论模型类

@IgnoreExtraProperties

公共类 CommentModel 实现可序列化 {

public CommentModel() {
}


String comment ,  commentDate , profilePic , commentUser ;

public CommentModel(String comment) {
    this.comment = comment;
}

public String getComment() {
    return this.comment;
}

public void setComment(String Comment) {
    this.comment = comment;
}

public String getCommentDate() {
    return this.commentDate;
}

public void setCommentDate(String commentDate) {
    commentDate = commentDate;
}

public String getProfilePic() {
    return profilePic;
}

public void setProfilePic(String profilePic) {
    this.profilePic = profilePic;
}

public String getCommentUser() {
    return commentUser;
}

public void setCommentUser(String commentUser) {
    commentUser = commentUser;
}

}

enter image description here


您的代码中的问题在于您的字段名称CommentModel类与数据库中的属性名称不同。你在你的CommentModel类一个名为comment但在你的数据库中我将​​其视为Comment这是不正确的。名称必须匹配。当您使用名为的 getter 时getComment(),Firebase 正在数据库中查找名为的字段comment并不是Comment。看小写的c字母与大写字母C?

有两种方法可以解决此问题。第一个是通过根据以下内容重命名字段来更改模型类Java 命名约定。所以你的模型类应该是这样的:

public class CommentModel {
    private String comment, commentDate, profilePic, commentUser;

    public CommentModel() {}

    public CommentModel(String comment, String commentDate, String profilePic, String commentUser) {
        this.comment = comment;
        this.commentDate = commentDate;
        this.profilePic = profilePic;
        this.commentUser = commentUser;
    }

    public String getComment() { return comment; }
    public String getCommentDate() { return commentDate; }
    public String getProfilePic() { return profilePic; }
    public String getCommentUser() { return commentUser; }
}

看这个例子,有private字段和公共吸气剂。还有一个更简单的解决方案,直接在公共字段上设置值,如下所示:

public class CommentModel {
    public String comment, commentDate, profilePic, commentUser;
}

现在只需删除当前数据并使用正确的名称再次添加即可。该解决方案仅在您处于测试阶段时才有效。

还有第二种方法,就是使用annotations。因此,如果您更喜欢使用私有字段和公共 getter,则应该使用属性名称注释仅在 getter 前面。所以你的CommentModel类应该如下所示:

public class CommentModel {
    private String comment, commentDate, profilePic, commentUser;

    public CommentModel() {}

    public CommentModel(String comment, String commentDate, String profilePic, String commentUser) {
        this.comment = comment;
        this.commentDate = commentDate;
        this.profilePic = profilePic;
        this.commentUser = commentUser;
    }

    @PropertyName("Comment")
    public String getComment() { return comment; }
    @PropertyName("CommentDate")
    public String getCommentDate() { return commentDate; }
    @PropertyName("ProfilePic")
    public String getProfilePic() { return profilePic; }
    @PropertyName("CommentUser")
    public String getCommentUser() { return commentUser; }
}

也不要忘记开始倾听变化。

附:在你的班级中,它应该是:

this.commentDate = commentDate;

and not:

commentDate = commentDate;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何读取 firestore 子集合并将其传递给 FirestoreRecyclerOptions 的相关文章

随机推荐