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

时间:2018-12-25 17:05:01

标签: java android firebase google-cloud-firestore

enter image description here我有包含根产品的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();

}

这是我的commentModel calss

@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

1 个答案:

答案 0 :(得分:1)

代码中的问题在于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; }
}

也不要忘记开始聆听更改。

P.S。在您的课堂上,应该是:

this.commentDate = commentDate;

而不是:

commentDate = commentDate;