gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:26:30 25 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 实现可序列化{

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

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

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

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

也不要忘记开始监听变化。

附注在你的类(class)中,它应该是:

this.commentDate = commentDate;

而不是:

commentDate = commentDate;

关于java - 如何读取 firestore 子集合并将其传递给 FirestoreRecyclerOptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53924189/

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