0

So I made a simple comment section for my app, the problem now is that when new comments are written they mix their timestamp and more importantly their nickname(user_id handlename). Here are some screenshots that I made between two phones:

Screenshot1 (test user) Screenshot2 my main phone(user2)

This is the code:

 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")
            .child("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());

 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();
    }
}


            }
        }
Timestamp code:
private void getTimeDifference() {

    long timeCurrent = System.currentTimeMillis() / 1000;
    long timePost = postTime / 1000;
    long timeDifference = timeCurrent - timePost;

    if (timeDifference < 60) {
        String time = timeDifference + "s";
        timestamp.setText(time);
    } else if (timeDifference < 3600) {
        String time = timeDifference / 60 + "m";
        timestamp.setText(time);
    } else if (timeDifference < 86400) {
        String time = timeDifference / 3600 + "h";
        timestamp.setText(time);
    } else if (timeDifference < 604800) {
        String time = timeDifference / 86400 + "d";
        timestamp.setText(time);
    } else if (timeDifference < 2419200) {
        String time = timeDifference / 604800 + "w";
        timestamp.setText(time);
    } else if (timeDifference < 29030400) {
        String time = timeDifference / 2419200 + "M";
        timestamp.setText(time);

    } else {
        String result = (String) DateUtils.getRelativeTimeSpanString(System.currentTimeMillis(), postTime, 0);
        String time = result.replace("In ", "");
        timestamp.setText(time);
    }
}

And finally how my database is structured:

enter image description here

1 Answers1

0

According to your second screenshot, I see that you want to order your elements descending by time but your results are mixed. This is because you are trying to filter your comments according to the user_id property and not according to the date_created property. To filter your comments according to a timestamp property descending, please see Frank's answer from this post.

Seeing your database, I'm affraind you'll need both filters but unfortunately Firebase realtime database does not support queries on multiple properties, supports only queries on a single child property. So a possible solution for this can be found here.

If you consider at some point to try using Cloud Firestore, please note that chaning multiple where() methods is permitted.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I will try this as soon as I am free, thank you for the answer, could you point out where should I change the child to filter by timestamp? –  Dec 03 '18 at 11:58
  • Your welcome! You can change `.child("user_id").equalTo(comment.getUser_id())` with `.child("date_created")`. – Alex Mamo Dec 03 '18 at 12:01
  • I tried your solution and no the issue is still happening, How can I just make it read the last added child inside comments child? –  Dec 03 '18 at 18:18
  • I can't understand why does the user_id mixes up when it is in a perfect order inside the database –  Dec 03 '18 at 18:35
  • The order in the Firebase Console is not reflected in your code. In the console, the element are sorted chronologically according to that pushed id which contains a time component. So if you have an order in your Console it does mean that the same order in your code. You should create the corresponding queries that can help you achieve this, as explained in my above answer. – Alex Mamo Dec 03 '18 at 19:32
  • Do you think there is something wrong with this part of the code that added about my timestamp? –  Dec 03 '18 at 20:38
  • I'm not sure what to say but this sounds as a new issue which basically should be considered another question that cannot be answered in a comment or using only the data above. As a personal guess, I think that you are using that date in a wrong way but in order to follow the rules of this comunity, please post another fresh question using a [MCVE](https://stackoverflow.com/help/mcve), so me and other users can help you. – Alex Mamo Dec 03 '18 at 20:45
  • I noticed something weird that when I comment twice from the same user than the issue occurs and the timestamp changes from a previous comment. –  Dec 03 '18 at 20:59