0

Please help me i need to add pagination in android firestore i have used base adapter to get events from firebase. i have read doc didn't find solution.

   DocumentSnapshot lastVisible = documentSnapshots.getDocuments()
            .get(documentSnapshots.size() -1);

    // Construct a new query starting at this document,
    // get the next 25 cities.
    Query next = db.collection("cities")
            .orderBy("population")
            .startAfter(lastVisible)
            .limit(25);

    // Use the query for pagination
    // ...

what i want is that i want to fetch next data and add it in my same list also i want to add event listeners in them and add data at the bottom of the list. How to call the next query for the same listener so that i could change data at runtime. Thanks in advance.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Adarsh Binjola
  • 216
  • 2
  • 10

1 Answers1

1

This is the easiest way in which you can paginate a query using a ListView and an ArrayAdapter on button click. A more complex approach would be found in this example where I have explained how you can paginate a query when user scrolls using a RecyclerView and a custom adapter.

In both examples, the key for solving the problem is to use the startAfter() method. So you can paginate queries by combining query cursors with the limit() method. You need to use the last document in a batch as the start of a cursor for the next batch.

If you'll prefer to use the second approach, I also recommend you take a look at this video for a better understanding.

If you want a solution for a real-time pagination, please check the following article:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • it paginate but fails when i i filtering data using price low to high ,suppose i have 20 data LIMIT is 10 i get 10 data this 10 data is filtered data.loadmore is called when i scroll down when i get the other 10 data i didnt get the filter data and the whole list is not filtered – Adarsh Binjola Jun 25 '18 at 17:49
  • found an easy way https://github.com/firebase/FirebaseUI-Android but still it cant be used in real time listeners – Adarsh Binjola Jun 25 '18 at 19:44
  • alex please tell me how to use OR query in firestore i have a situation where i have to add filters in my app suppose i add filter of blue and white color .i want answer as all data whith white color OR blue color but currently using whereEqualTo it is giving result of data which have only both colors white and blue. – Adarsh Binjola Jun 25 '18 at 19:49
  • How to `add filters in my app` is basically another question. In order to follow the rules of this comunity, please post another fresh question, so me and other users can help you. – Alex Mamo Jun 26 '18 at 06:26