如何读取 firestore 子集合并将其传递给 FirestoreRecyclerOptions [英] how to read firestore sub-collection and pass it to FirestoreRecyclerOptions

查看:17
本文介绍了如何读取 firestore 子集合并将其传递给 FirestoreRecyclerOptions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含根产品的 firestore 数据库,每个产品都有评论"集合,所以我将所有用户对该产品的评论存储在其中,但是当查询此评论时,我从 Firestore 获取空值或零快照的集合

I 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 实现了 Serializable {

public class CommentModel implements 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;
}

}

推荐答案

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

The problem in your code lies in the fact that the name of the fields in your CommentModel class are different than the name of the properties in your database. You have in your CommentModel class a field named comment but in your database I see it as Comment and this is not correct. The names must match. When you are using a getter named getComment(), Firebase is looking in the database for a field named comment and not Comment. See the lowercase c letter vs. capital letter C?

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

There are two ways in which you can solve this problem. The first one would be to change your model class by renaming the fields according to the Java Naming Conventions. So you model class should look like this:

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.还有一个更简单的解决方案,直接在公共字段上设置值,如下所示:

See in this example, there are private fields and public getters. There is also a simpler solution, to set the value directly on public fields like this:

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

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

Now just remove the current data and add it again using the correct names. This solution will work only if you are in testing phase.

还有第二种方法,即使用注解.因此,如果您更喜欢使用私有字段和公共 getter,则应使用 PropertyName 注释只在getter 前面.所以你的 CommentModel 类应该是这样的:

There is also the second approach, which is to use annotations. So if you prefer to use private fields and public getters, you should use the PropertyName annotation only in front of the getter. So your CommentModel class should look like this:

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

不要忘记开始倾听变化.

Don't also forget to start listening for changes.

附言在你的课上,它应该是:

P.S. In your class, it should be:

this.commentDate = commentDate;

而不是:

commentDate = commentDate;

这篇关于如何读取 firestore 子集合并将其传递给 FirestoreRecyclerOptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆