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

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

问题描述

我有包含根产品的firestore数据库,并且每个产品都有"comments"集合,因此我在其中存储了所有用户对此产品的评论,但是当对此评论进行查询时,集合中,我从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();

}

这是我的commentModel calss

and here is my commentModel calss

@IgnoreExtraProperties

公共类CommentModel实现了可序列化的{

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()的吸气剂时,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字段和公共获取器.还有一个更简单的解决方案,可以直接在公共字段上设置值,如下所示:

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.

还有第二种方法,即使用annotations.因此,如果您更喜欢使用私有字段和公共获取程序,则应使用

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.在您的课堂上,应该是:

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

this.commentDate = commentDate;

而不是:

commentDate = commentDate;

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

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