【问题标题】:how to read firestore sub-collection and pass it to FirestoreRecyclerOptions如何读取 firestore 子集合并将其传递给 FirestoreRecyclerOptions
【发布时间】:2019-05-24 06:02:52
【问题描述】:

我有带有根产品的firestore数据库,每个产品都有集合'cmets',所以我在其中存储了所有用户关于这个产品的cmets,但是当查询这个cmets子集合时,我得到空值或零快照火炉

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

}

【问题讨论】:

  • 请添加您的comments 集合的结构并确认您已开始监听更改。
  • 我添加了 cmets 屏幕截图和评论模型类,是的,我使用代码开始监听 @Override protected void onStart() { super.onStart();适配器.startListening(); } 但在 recyclerview 中获取空值

标签: java android firebase google-cloud-firestore


【解决方案1】:

代码中的问题在于CommentModel 类中的字段名称与数据库中属性的名称不同。您在CommentModel 类中有一个名为comment 的字段,但在您的数据库中我将​​其视为Comment,这是不正确的。名称必须匹配。当您使用名为 getComment() 的 getter 时,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 字段和公共getter。还有一个更简单的解决方案,直接在公共字段上设置值,如下所示:

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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2021-06-30
    • 2019-10-03
    • 2013-08-21
    • 2023-03-18
    • 2015-12-27
    相关资源
    最近更新 更多