1

I am using Firebase Recycle Adapter to display my messages. I want to display newly received message on top of the recycle view. E.g newly sent/received message by Jfi.. should be on top.

screenshot

Here is my db structure.

db structure

Code:

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    Query query = rootRef.child("messages").child(messageSenderId).child(messageReceiverId);


    FirebaseRecyclerOptions<ChatMessage> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<ChatMessage>()
            .setQuery(query, ChatMessage.class)
            .build();

Thanks

john
  • 2,324
  • 3
  • 20
  • 37

1 Answers1

2

you can store the messages into Firebase with a timestamp, then when you retrieve all the data of the message, compare it to the rest of the messages and sort it by the date, and they will display at the top the newest ones, or just invert the RecyclerView and you will see the latest messages at the top of the RecyclerView

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
myRecyclerView.setLayoutManager(linearLayoutManager);

EDIT

You can compare the values of each message and sort them by the timestamp you get from Firebase

 public class timeStampSort implements Comparator<YourModel> {

        @Override
        public int compare(YourModel o1, YourModel o2) {
            return Long.compare(o2.getTimestamp(), o1.getTimestamp());

        }

    }

and then before calling mAdapter = new MessagesAdapter.... do this

Collections.sort(yourArray, new timeStampSort());

That should order your messages with the timestamp from each one without having to invert the RecyclerView

The above sort can be done if you already made another orderByChild in your query and you can't do more than once.

But, as Frank suggests, it's much more efficient to do it with orderByChild, so you can only do this

Query query = rootRef.child("messages").child(messageSenderId).child(messageReceiverId).orderByChild("timestamp");
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
  • I have tried linearLayoutManager.setStackFromEnd(true); but it didn't work. timestamp sounds good :) – john Dec 20 '18 at 22:09
  • yes, pushing the messages with a timestamp can then be retrieved and ordered with a collection.sort , use ServerValue.TIMESTAMP – Gastón Saillén Dec 20 '18 at 22:10
  • check this https://stackoverflow.com/questions/41976747/android-order-firebase-array-by-timestamp and this https://stackoverflow.com/questions/5927109/sort-objects-in-arraylist-by-date – Gastón Saillén Dec 20 '18 at 22:12
  • Great. Let me check. – john Dec 20 '18 at 22:13
  • I have used ServerValue.TIMESTAMP and its working great. But I am still unable to order by timestamp. Kindly let me know what I am missing. DB: https://imgur.com/a/zzjn7dw Code: https://imgur.com/a/Ta9dBn4 – john Dec 20 '18 at 23:23
  • 2
    Firebase Database queries are always ascending. So if you want the newest node to be on top, you'll want to store for example an inverted timestamp and order on that. E.g. `nodeRef.child("invertedTimestamp").setValue(0 - System.currentTimeMillis())` See https://stackoverflow.com/questions/44442816/firebase-date-order-reverse. Luckily Android makes it easy to do the inversion in the UI, which I completely forgot about, but luckily Gastón didn't. ;-) For more on these, see https://stackoverflow.com/search?q=%5Bfirebaseui%5D+reverse – Frank van Puffelen Dec 20 '18 at 23:24
  • To order the results from your query by timestamp: `Query query = rootRef.child("messages").child(messageSenderId).child(messageReceiverId).orderByChild("timestamp");` – Frank van Puffelen Dec 20 '18 at 23:30
  • Thanks a lot Frank, I'm just at the same problem with another app that I have, this for sure will help ! :) , I just added the Collection sort since in my use case I queried before with orderByChild and cant use it again, but it is always better to use it with the timestamp, will include this in the answer as well – Gastón Saillén Dec 20 '18 at 23:31