0

The comment section of my application is not working properly, the childs are mixing up by their names and the timestamp, the content inside of them it is in correct order, here are all of my code inside Pastebin links (if necessary to see the whole code), also I provided the chunks I think that the problem is in.

1) CommentAdapter.java Activity (https://pastebin.com/m7LHDUDF) Take note of this block of code:

private void getHandleName(final ViewHolder viewHolder) {
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
    Log.d(TAG, "getHandleName: checking comment userID" + viewHolder.comment.getUser_id());
    Query query;
    query = reference
            .child("data")
            .child("-Kxzyb5JsUPhsMQAb84X")
            .child("users")
            .orderByChild("user_id")
            .equalTo(viewHolder.comment.getUser_id());

    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
                viewHolder.handleName.setText(singleSnapshot.getValue(User.class).getHandlename());
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

2) CommentRecylerViewAdapter (https://pastebin.com/Tb7L9EVD)

 private void getHandleName(final CommentViewHolder viewHolder, Comment comment) {
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
    Log.d(TAG, "getHandleName: checking comment userID" + comment.getUser_id());
    Query query = reference
            .child("data")
            .child("-Kxzyb5JsUPhsMQAb84X")
            .child("users")
            .orderByChild("user_id")
            .equalTo(comment.getUser_id());


    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
                viewHolder.handleName.setText(singleSnapshot.getValue(User.class).getHandlename());
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

3)Chunk of code for adding the comment: ` private void addComment() {

    if (commentText.getText().toString().isEmpty()) {
        Toast.makeText(ViewPostActivity.this, "Please enter your comment", Toast.LENGTH_SHORT).show();
    } else {
        String currentUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
        String commentID = reference.push().getKey();

        Comment comment = new Comment();
        comment.setCaption(commentText.getText().toString());
        comment.setDate_created(System.currentTimeMillis());
        comment.setUser_id(currentUserID);

        reference.child("data").child("-Kxzyb5JsUPhsMQAb84X").child("comments").child(postID).child(commentID).setValue(comment);
        setNumComment();
        setNumPointCurrentUser();
        setNumPointUser();
        setNumPointPost();
    }
}`

4) **And Finally, the comment model Activity** which I think is ok(https://pastebin.com/VaYV3Gv3)

Here is a screenshot of my database:

  1. -Kxzyb5JsUPhsMQAb84X is the root of my database
  2. comments is where all the comments are store per post
  3. -LSv6lXZml-Cf0GX3i5q randomly generated child that stores the other content such as Timestamps,user id and content

Database

Here is the screenshot of my phone showing the incorrect order of the comments:

Phone Screenshot

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    There's also a `-LSym...` key in the tree, which I assume is the comment ID. There's no way for Firebase to order child nodes over double nested dynamic keys. What you pass into `orderByChild()` must exist at a fixed path under each child node of the location you query. See https://stackoverflow.com/questions/27207059/firebase-query-double-nested – Frank van Puffelen Dec 05 '18 at 15:15
  • @FrankvanPuffelen So I am afraid that this can't be solved, any suggestion that will help me with this headache or point out a another way of handling comments. –  Dec 05 '18 at 15:21
  • Firebase Database searches across a list of child nodes. Right now you have a tree, which it can't search across. If you want to search across all comments to all posts, you need to have a non-nested list of all comments. – Frank van Puffelen Dec 05 '18 at 17:56

0 Answers0