gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:04:07 24 4
gpt4 key购买 nike

enter image description here我有根产品的 firestore 数据库,每个产品都有“评论”集合,所以我在其中存储了所有用户对该产品的评论,但是当查询这个评论子集合时,我从 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 类

@IgnoreExtraProperties

公共(public)类 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;
}

enter image description here

最佳答案

您的代码中的问题在于您的 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字段和public getters。还有一个更简单的解决方案,直接在公共(public)字段上设置值,如下所示:

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

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

还有第二种方式,就是使用annotations。因此,如果您更喜欢使用私有(private)字段和公共(public) getter,则应该使用 PropertyName仅在 setter/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;
}

@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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57279727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com