0

I'm using push() method to create unique ids for database but when I create an id with push method its added to bottom of tree but I want it to be on top. How can I do that?

For example in this picture bottom one is newest one I want it to be on top. How can I do that?

Picture

I want this because I use RecyclerView with Firebase adapter and adapter start to read from top.

edit: how to get data from bottom to top from firebase?

This question is what I want but there is no useful answer.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
uzaysan
  • 583
  • 1
  • 4
  • 18
  • That's how the database structure works. I'm not sure if we can do some magic on the Android for the server side. – ʍѳђઽ૯ท Oct 08 '18 at 20:10
  • @Mohsen thanks for answer .So do you know how to make recycler view adapter to start reading from bottom not top? – uzaysan Oct 08 '18 at 20:20
  • Are you looking for better performance on the android side? Quick loading on the `RecyclerView`? – ʍѳђઽ૯ท Oct 08 '18 at 20:21
  • @Mohsen No I have a recycler view.I load data from firebase but recycler view start reading data from top.But newest added item is bottom.I want to show newest item in top of recycler view like old instagram. – uzaysan Oct 08 '18 at 20:25
  • Duplicate?: https://stackoverflow.com/questions/47765213/how-to-get-the-most-recent-data-from-firebase-database – ʍѳђઽ૯ท Oct 08 '18 at 20:27
  • @Mohsen its not usefull for me.I dont know how can i explain what i want because my english is not good.I want my recycler view to load data fro firebase but i dont want it to load from old to new i want it to load new to load.And firebase push method write data to bottom of data tree. For example if i have 3 key under a parent my new push key will be 4th key.And i want forth key to be loaded first. newest to oldest. – uzaysan Oct 08 '18 at 20:34
  • 1
    @UzaySan If what you tried to show the items in reverse chronological order does't work, update your question to include [minimal, complete code that reproduces what you tried](http://stackoverflow.com/help/mcve). – Frank van Puffelen Oct 08 '18 at 20:54

2 Answers2

0

you can write a simple query to get data as you need it add date parameter to your object then query the data by the date

 DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

Query query = reference.child("data").orderByChild("date");
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            for (DataSnapshot child: dataSnapshot.getChildren()) {
                // fill recyclarView with data
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});
Falah H. Abbas
  • 512
  • 5
  • 11
0

I'm using push() method to create unique ids for database but When i create an id with push method its added to bottom of tree but i want it to be on top

When you are using the push() method, the objects in the Firebase Console are ordered chronologically and unfortunately there is nothing you can do about it. This order cannot be changed. If you want to display the items from the datababse in a RecyclerView and hypothetically speaking, you would have been able to change the order in the console, it doesn't mean that this order is the same with order in the RecyclerView. To display your items in a particular order, I recommend you to use a Query and sort them according to a timestamp property. For that, please see Frank van Puffelen's answer from this post and if you are using the Firebase-UI library, please see my answer from this post.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • yes reversing layout worked fine. But what if im doing paging.For example reference.limittolast(15); Then when user reached bottom of recyclerview load another 15.Does reversing layout still works in this case? – uzaysan Oct 09 '18 at 10:27
  • Good to hear that it worked fine but let's stick to one question at a time. What you are asking is basically another question. You should make your own attempt and ask another question if something else comes up, so me and other users can help you. – Alex Mamo Oct 09 '18 at 10:47