0

i have been working to retrive data from firebase realtime database to listview in my application but when it comes to listview i wanted to show the last retrived data in top of the listview.this is my code for retriving data and setting to listview,using this code the data is retrived to bottom of listview

        list = new ArrayList<>();
        adapter = new ArrayAdapter<String>(this, R.layout.notifi, R.id.notifi, list);
        ValueEventListener postListener = new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    notif = ds.getValue(notificationclass.class);
                    list.add(notif.getNotification());
                }
                listView.setAdapter(adapter);


            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        };
        databaseReference.addValueEventListener(postListener);
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Can you share your `notificationclass.getNotification()` function here please? – ravi May 17 '20 at 06:12
  • There are many ways to do this, such as [reversing the list](https://stackoverflow.com/a/56347763), [storing a property with an inverted value](https://stackoverflow.com/a/34158197), [telling the listview to invert its layout](https://stackoverflow.com/a/37399475), or [adding the items to the start of the list](https://stackoverflow.com/a/52813907). – Frank van Puffelen May 17 '20 at 06:19

1 Answers1

0

There is no default mechanism to read data in descending order. But you can sort your data byChild, byKey and byValue. Check details Here

Nipon Roy
  • 146
  • 6