[英]how to read firestore sub-collection and pass it to FirestoreRecyclerOptions
我有帶有根產品的 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();
}
這是我的評論模型類
@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 命名約定重命名字段來更改模型類。 所以你的模型類應該是這樣的:
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
字段和公共 getter。 還有一個更簡單的解決方案,直接在公共字段上設置值,如下所示:
public class CommentModel {
public String comment, commentDate, profilePic, commentUser;
}
現在只需刪除當前數據並使用正確的名稱再次添加它。 此解決方案僅在您處於測試階段時才有效。
還有第二種方法,即使用annotations
。 因此,如果您更喜歡使用私有字段和公共 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; }
}
也不要忘記開始傾聽變化。
PS 在你的課上,應該是:
this.commentDate = commentDate;
並不是:
commentDate = commentDate;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.