我的应用从Firebase尝试获取空评论,因此许多方法似乎无法获得正确的方法。我被困在这里好几天了

**This is my firebase database structure**This [This is my firebase data base comment structure ][2] My app works well no errors except logical error, I can't seem to get the accurate method to readback/fetch back comments already stored in my app's database, please I do need aid over here I am stuck. I need a method that would get back the total comments on a post and display the numbers beside the comment Icon. Your help is highly appreciated.

下面是显示和显示评论数的PostAdapter的代码

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.MyViewHolder> {

    Context mCtx;
    List<Post> mData;
    private DatabaseReference likesRef;
    Boolean likesChecker = false;
    FirebaseUser currentUser;
    FirebaseAuth mAuth;
    String currentUserIDX;

    Boolean commentsChecker = false;
    static String COMMENT_KEY = "Comments";


    public PostAdapter(Context mCtx, List<Post> mData) {
        this.mCtx = mCtx;
        this.mData = mData;

    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        mAuth = FirebaseAuth.getInstance();
        currentUserIDX = mAuth.getCurrentUser().getUid();
        //  Inflate the row_post_item XML
        View row = LayoutInflater.from(mCtx).inflate(R.layout.row_post_item, parent, false);
        likesRef = FirebaseDatabase.getInstance().getReference().child("Likes");


        return new MyViewHolder(row);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {


        final String PostKey = mData.get(position).getPostKey();
        final String commentPost = mData.get(position).getCommentPost();

        holder.showCommentButtonStatus(commentPost);

        holder.displayNoOfComments.setText(commentPost + " Comments");

}
        public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView txTittle;
        ImageView imgPost;
        ImageView imgPostProfile;
        int countLikes;
        int countComments;
        String currentUserID;
        DatabaseReference LikeRef, commentsRef;

        ImageButton btnLikePost, btnComment, btnShare;
        TextView displayNoOfLikes, displayNoOfComments;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            txTittle = itemView.findViewById(R.id.row_post_tittle);
            imgPost = itemView.findViewById(R.id.row_post_imageID);
            imgPostProfile = itemView.findViewById(R.id.row_post_userProfImg);

            LikeRef = FirebaseDatabase.getInstance().getReference().child("Likes");
            currentUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();
            commentsRef = FirebaseDatabase.getInstance().getReference().child("uID");


            btnLikePost = itemView.findViewById(R.id.row_post_like);
            btnLikePost.setBackgroundColor(Color.TRANSPARENT);
            btnComment = itemView.findViewById(R.id.row_post_comment);
            btnComment.setBackgroundColor(Color.TRANSPARENT);
            btnShare = itemView.findViewById(R.id.row_post_share);

            displayNoOfLikes = itemView.findViewById(R.id.row_post_display_no_of_likes);
            displayNoOfComments = itemView.findViewById(R.id.row_post_display_no_of_comment);


            // I came from postDetails Activity to set things up.
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent postDetailsActivity = new Intent(mCtx, PostDetailsActivity.class);
                    int position = getAdapterPosition();

                    postDetailsActivity.putExtra("tittle", mData.get(position).getTittle());
                    postDetailsActivity.putExtra("postImage", mData.get(position).getPictures());
                    postDetailsActivity.putExtra("description", mData.get(position).getDescription());
                    postDetailsActivity.putExtra("postKey", mData.get(position).getPostKey());
                    postDetailsActivity.putExtra("userPhoto", mData.get(position).getUserPhotos());

                    //postDetailsActivity.putExtra("userName", mData.get(position).getUserName());

                    long timestamp = (long) mData.get(position).getTimestamp();
                    postDetailsActivity.putExtra("postDate", timestamp);
                    mCtx.startActivity(postDetailsActivity);


                }
            });


        }
//          Intent intent = getIntent();
//          PostKey = intent.getStringExtra("postKey");

        public void showCommentButtonStatus(final String uID) {

            commentsRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    // Retrieve user Information
                    dataSnapshot.child(String.valueOf(uID)).hasChild(currentUserID);
                    String CommentPost = (String) dataSnapshot.child("uID").getValue();
                    mAuth.getCurrentUser().getUid();
                    dataSnapshot.child(String.valueOf(CommentPost)).hasChild(currentUserID);
                    countComments = (int) dataSnapshot.child(String.valueOf(CommentPost)).getChildrenCount();
                    displayNoOfComments.setText(CommentPost);
//

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
});
        }

然后下面的代码来自我的应用程序的评论适配器部分,我试图找出没有正面结果的地方来获取评论并显示计数,我的意思是对帖子发表评论的人数。

public class CommentsAdapter extends RecyclerView.Adapter<CommentsAdapter.CommentViewHolder> {

    private Context mCtx;
    private List<Comments> mData;
    FirebaseAuth mAuth;
    String currentUserIDX;
    private DatabaseReference likesRef, disLikesRef, CommentsRef;
    Boolean likesChecker = false;
    Boolean dislikesChecker = false;



    public CommentsAdapter(Context mCtx, List<Comments> mData) {
        this.mCtx = mCtx;
        this.mData = mData;
    }

    @NonNull
    @Override
    public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {


        mAuth = FirebaseAuth.getInstance();
        currentUserIDX = mAuth.getCurrentUser().getUid();

        View row = LayoutInflater.from(mCtx).inflate(R.layout.row_comments, parent, false);

        likesRef = FirebaseDatabase.getInstance().getReference().child("Likes");
        disLikesRef = FirebaseDatabase.getInstance().getReference().child("Dislikes");
        CommentsRef = FirebaseDatabase.getInstance().getReference().child("uID");
        return new CommentViewHolder(row);


        // getIntent().getExtras().getString("commentKey");
    }

    @Override
    public void onBindViewHolder(@NonNull CommentViewHolder holder, int position) {

        final String CommentKey = mData.get(position).getCommentKey();

        Glide.with(mCtx).load(mData.get(position).getUimg()).into(holder.img_user);
        holder.txt_name.setText(mData.get(position).getUname());
        holder.txt_content.setText(mData.get(position).getContent());
        holder.txt_date.setText(timestampToString((Long) mData.get(position).getTimestamp()));


        holder.reply.setText(mData.get(position).getReply());



        holder.setLikeCommentBtnStatus(CommentKey);

        holder.setUnlikeCommentBtnStatus(CommentKey);

        holder.setReplyCommentBtnStatus();




    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public  class CommentViewHolder extends RecyclerView.ViewHolder {

        ImageView img_user;
        TextView txt_name, txt_content, txt_date;
        ImageButton btnLikeComment, btnDislikeComment;


        TextView displayNoOfLikes, displayNoOfDislikes, reply;


        int countLikes;
        int countDislikes;
        String currentUserID;
        DatabaseReference LikeRef, DislikeRef, ReplyRef, commentRef;

        public CommentViewHolder(View itemView){
            super(itemView);

            img_user = itemView.findViewById(R.id.comment_user_img);
            txt_name = itemView.findViewById(R.id.comment_username);
            txt_content = itemView.findViewById(R.id.comment_content);
            txt_date = itemView.findViewById(R.id.comment_date);

            LikeRef = FirebaseDatabase.getInstance().getReference().child("Likes");

            DislikeRef = FirebaseDatabase.getInstance().getReference().child("Dislikes");
            ReplyRef = FirebaseDatabase.getInstance().getReference().child("Replies");
            commentRef = FirebaseDatabase.getInstance().getReference().child("uID");


            btnLikeComment = itemView.findViewById(R.id.comment_like_post);
            btnLikeComment.setBackgroundColor(Color.TRANSPARENT);

            btnDislikeComment = itemView.findViewById(R.id.comment_dislike_post);
            btnDislikeComment.setBackgroundColor(Color.TRANSPARENT);

            currentUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();

            displayNoOfLikes = itemView.findViewById(R.id.comment_like_post_display_no_of_likes);
            displayNoOfLikes = itemView.findViewById(R.id.comment_like_post_display_no_of_dislikes);
            reply = itemView.findViewById(R.id.comment_reply);




        }
评论