how to read firestore sub-collection and pass it to FirestoreRecyclerOptions
Solution 1:
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 your CommentModel
class a field named comment
but in your database I see it as Comment
and this is not correct. The names must match. When you are using a getter named getComment()
, Firebase is looking in the database for a field named comment
and not Comment
. See the lowercase c
letter vs. capital letter C
?
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:
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; }
}
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:
public class CommentModel {
public String comment, commentDate, profilePic, commentUser;
}
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 your CommentModel
class should look like this:
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; }
}
Don't also forget to start listening for changes.
P.S. In your class, it should be:
this.commentDate = commentDate;
and not:
commentDate = commentDate;