I am developing an android chat application in which I need to order the conversation details by the timestamp. My firebase data structure is mentioned below .
Now I want to retrieve and show the data in decending/latest order 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("Timestamp")
.limitToLast(5);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(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());
}
}