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

Java 如何读取firestore子集合并将其传递给FirestoreRecyclerOptions,java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,我有根产品的firestore数据库,每个产品都有集合“comments”,所以我在其中存储了所有用户对此产品的评论,但当查询此评论子集合时,我从firestore获得空值或零快照 private void getCommentObject(){ query = FirebaseFirestore.getInstance() .collection("products").document(docID).collection("comments");

我有根产品的firestore数据库,每个产品都有集合“comments”,所以我在其中存储了所有用户对此产品的评论,但当查询此评论子集合时,我从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实现了可序列化{

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

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

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; }
}
public class CommentModel {
    public String comment, commentDate, profilePic, commentUser;
}
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; }
}
在本例中,有
private
字段和public getter。还有一个更简单的解决方案,直接在公共字段上设置值,如下所示:

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; }
}
public class CommentModel {
    public String comment, commentDate, profilePic, commentUser;
}
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; }
}
现在只需删除当前数据并使用正确的名称再次添加它。此解决方案仅在您处于测试阶段时有效

还有第二种方法,即使用
注释
。因此,如果您更喜欢使用私有字段和公共getter,那么应该只在getter前面使用注释。因此,您的
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;
    }

    public String getComment() { return comment; }
    public String getCommentDate() { return commentDate; }
    public String getProfilePic() { return profilePic; }
    public String getCommentUser() { return commentUser; }
}
public class CommentModel {
    public String comment, commentDate, profilePic, commentUser;
}
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;

请添加
注释
集合的结构,并确认已开始侦听更改。我已添加注释屏幕截图和注释模型类,是的,我正在使用代码开始侦听@Override protected void onStart(){super.onStart();adapter.startListening();}但在recyclerview中获取空值