how to read firestore sub-collection and pass it t

2020-01-26 12:04发布

enter image description hereI 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;
}

}

enter image description here

1条回答
家丑人穷心不美
2楼-- · 2020-01-26 12:59

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;
查看更多
登录 后发表回答