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 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?
有两种方法可以解决此问题.第一个是通过根据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; }}
在此示例中,您可以看到私有字段和公共getter.还有一个更简单的解决方案,直接在公共字段上设置值,如下所示:
public class CommentModel { public String comment, commentDate, profilePic, commentUser;}
现在只需删除当前数据并使用正确的名称再次添加.只有在您处于测试阶段时,此解决方案才有效.
还有第二种方法,即使用注释.因此,如果您更喜欢使用私有字段和公共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;
总结 以上是内存溢出为你收集整理的java – 如何读取firestore子集合并将其传递给FirestoreRecyclerOptions全部内容,希望文章能够帮你解决java – 如何读取firestore子集合并将其传递给FirestoreRecyclerOptions所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)