Search code examples
javaandroidfirebasefirebase-realtime-databaseandroid-paging

Get only the records between the fifth and tenth records from a realtime database


I use:

commentRef.child(poi.Id).child("text").orderByKey().limitToLast(5).addListenerForSingleValueEvent(new ValueEventListener() {   

to get the last 5 records.

Now I need that when I click on a "more" button, to receive the 5 previous recordings from the server. I could increase the limit to 10 registrations, but this would mean receiving the last 5 registrations again, which would increase the data traffic and therefore implicitly the costs.

I would prefer to receive only the 5 previous recordings and add them to my list of the already existing ones.

How could I get the database records from a certain beach? Not all the records in the node, but those from position 5 to position 10, for example.


Solution

  • What you are looking for is called pagination or infinite scroll. You can achieve this by using Query#startAt() and Query#endAt() methods. In this way, you'll be able to load the data in smaller chunks by specifying the starting element and the element to stop at. I'm not aware of an official example, but this topic has been covered multiple times before:

    If you understand Kotlin, then I think that this resource will help. Here is the corresponding repo.