skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    I post my solution for those interested

    commentRef = FirebaseDatabase.getInstance().getReference("Posts");
    commentRef.child(post.Id).child("text").orderByKey().endAt(lastKey).
    limitToLast(showItems).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) { ...
    

    lastkey is the value of the record from the last position up to which I want to obtain results and showItems is the number of records that I want to receive in order to display them in recyclerview.


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search