java - 如何读取Firestore子集合并将其传递给FirestoreRecyclerOptions-LMLPHP我有包含根产品的firestore数据库,每个产品都有“评论”集合,因此我在其中存储了所有用户对此产品的评论,但是当查询此评论子集合时,我从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实现Serializable {

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;
}


}

java - 如何读取Firestore子集合并将其传递给FirestoreRecyclerOptions-LMLPHP

最佳答案

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

有两种方法可以解决此问题。第一个是通过根据Java Naming Conventions重命名字段来更改模型类。因此,您的模型类应如下所示:

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之前使用PropertyName批注。因此,您的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;


并不是:

commentDate = commentDate;

09-28 04:18