2

I am developing an Android chat application in which I need to order the conversation details by the date. My firebase data structure is mentioned below.

Database structure

Now I want to retrieve and show the data on the latest date on my RecyclerView from Firebase Realtime Database based on timestamp.

I have tried the following approaches.

final DatabaseReference nm =
        FirebaseDatabase.getInstance().getReference("Transaction");

Query query = nm.orderByChild("Date").limitToFirst(5);
;

query.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        listData.clear();
        if (dataSnapshot.exists()) {
            for (DataSnapshot npsnapshot : dataSnapshot.getChildren()) {
                Transaction ld = npsnapshot.getValue(Transaction.class);

                listData.add(ld);
            }
            Tadapter = new TransactionAdapter(listData);
            rv.setAdapter(Tadapter);
            Log.d(TAG, "Total Count" + Tadapter.getItemCount());
        }
    }
}
AV114
  • 41
  • 7

2 Answers2

4

I am developing an android chat application in which I need to order the conversation details by the date.

As I see in your screenshot, your Date property is of type String. This means that you cannot call:

.orderByChild("Date")

And expect to behave as it was a Timestamp. When you order String elements, the order that you get is always lexicographically. This means that Strings doesn't consider any sort of numeric values when sorting, especially when it comes to the dates, even if the dates contain numbers, as your first element does:

Date: "30/7/2021"

So using String values when querying your database it's not an option. However, I see you already have a Timestamp property. Maybe on that property, it was supposed to do the ordering. If that was not the case, I suggest you change the type of the Date property from String to Timestamp, as explained in my answer from the following post:

Now I want to retrieve and show the data on the latest date on my RecyclerView

This means that most likely you need to reverse the order, meaning that all your transactions have to be displayed in your RecyclerView descending. In this case, there are a few options that you have, either on the server or on the client.

Assuming that you have changed the type of your Date property from String to Timestamp, then you can simply consider storing an inverted Timestamp value like this:

Firebase-root
  |
  --- transactions
        |
        --- 1
            |
            --- Date: 1627714194
            |
            --- invertedDate: -1627714194

See, the invertedDate property holds a negative value. Since by default, the elements are ordered ascending, to be able to order the transaction desecendiong, you should simply use:

Query query = nm.orderByChild("invertedDate").limitToFirst(5);

On the other hand, there are some workarounds that can be made to achieve the same thing on the client, as explained in my answer from the following post:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
0
Query query = nm.orderByChild("Date").limitToFirst(5);

Firebase realtime database sorts in ascending order that means those 5 nodes that you'll receive will be the oldest.

I want to retrieve and show the data in latest date

Try using limitToLast instead which will return the last 5 documents after ordering the nodes by Date field i.e. the 5 latest nodes.

Query query = nm.orderByChild("Date").limitToLast(5);

You can read more about that at sorting and filtering data.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84