I have firestore database with root products and every products has collection 'comments' so i stored in it all users comments about this product , but when query on this comments sub-collection i get null values or zero snapshots from 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();
}
and here is my commentModel calss
@IgnoreExtraProperties
public class CommentModel implements 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;
}
}
The problem in your code lies in the fact that the name of the fields in your
CommentModel
class are different than the name of the properties in your database. You have in yourCommentModel
class a field namedcomment
but in your database I see it asComment
and this is not correct. The names must match. When you are using a getter namedgetComment()
, Firebase is looking in the database for a field namedcomment
and notComment
. See the lowercasec
letter vs. capital letterC
?There are two ways in which you can solve this problem. The first one would be to change your model class by renaming the fields according to the Java Naming Conventions. So you model class should look like this:
See in this example, there are
private
fields and public getters. There is also a simpler solution, to set the value directly on public fields like this:Now just remove the current data and add it again using the correct names. This solution will work only if you are in testing phase.
There is also the second approach, which is to use
annotations
. So if you prefer to use private fields and public getters, you should use the PropertyName annotation only in front of the getter. So yourCommentModel
class should look like this:Don't also forget to start listening for changes.
P.S. In your class, it should be:
and not: