1

I am using FirestoreRecyclerAdaper with the normal form of retrieving and showing data.

my data becomes bigger and I want to make pagination in it, but I didn't find any tutorial to this point. I read documentations but nothing show how to do that because their is only one adapter and 2 queries and FirestorRecyclerAdaper accept only one query. so is their a solution without changing my code?

final Query query = firebaseFirestore
         .collection("Ads")
         .orderBy("creationDate", Query.Direction.DESCENDING).limit(5);
     query.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
          @Override
          public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
          DocumentSnapshot lastVisible = queryDocumentSnapshots
              .getDocuments().get(queryDocumentSnapshots.size() - 1);


          Query next = firebaseFirestore.collection("Ads")
              .orderBy("creationDate")
              .startAfter(lastVisible)
              .limit(10);


          }

     });
 options = new FirestoreRecyclerOptions
         .Builder<MyAdCard>()
         .setQuery(query, MyAdCard.class)
         .build();

     dapter = new FirestoreRecyclerAdapter<MyAdCard, AllCardViewHolder>(options) {
          @Override
          protected void onBindViewHolder(@NonNull final AllCardViewHolder holder, final int position, @NonNull MyAdCard model) {
          holder.publicAd_discription.setText(model.getMyAddiscriptiontxt());



          }

          @NonNull
          @Override
          public AllCardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
          View view = LayoutInflater.from(parent.getContext())
              .inflate(R.layout.cardview_categoryads, parent, false);
          return new AllCardViewHolder(view);

          }
     };
     categoryAdsRV.setAdapter(dapter);
 private class AllCardViewHolder extends RecyclerView.ViewHolder {
     public AllCardViewHolder(View itemView) {
          super(itemView);
     }

     TextView publicAd_discription = itemView.findViewById(R.id.publicAd_Discription);
     }

     @Override
     protected void onStart() {
     super.onStart();
     dapter.startListening();

     }

     @Override
     protected void onStop() {
     super.onStop();
     if ( dapter != null ) dapter.stopListening();
     }
Amr Mahmoud
  • 122
  • 1
  • 15
  • 1
    **[This](https://stackoverflow.com/questions/50741958/how-to-paginate-firestore-with-android)** is a recommended way in which you can paginate queries by combining query cursors with the limit() method. I also recommend you take a look at this **[video](https://www.youtube.com/watch?v=KdgKvLll07s)** for a better understanding. – Alex Mamo Sep 09 '18 at 10:33
  • 1
    yes , I saw your video and works good, but that means I had to change all of my codes and this is what I must do. but I had some problems in your code. first: I can't get id which resemble the document in the database. second I notices that I have below some items repeated from the first items from my recycler view. – Amr Mahmoud Sep 09 '18 at 12:49
  • In this case, please post another question containing an [MCVE](https://stackoverflow.com/help/mcve), so me and other users can help you. – Alex Mamo Sep 09 '18 at 14:07
  • 1
    I posted it as question, can u take alook at it? https://stackoverflow.com/questions/52245510/how-to-get-document-id-of-the-postion-of-recycler-view-item?noredirect=1#comment91439333_52245510 – Amr Mahmoud Sep 09 '18 at 14:44

1 Answers1

1

There is nothing you can do here without changing your code significantly.

There is a FirestorePagingAdapter from Firebase-UI that may help you with the changes you'll need to make.

There is also a sample code I wrote with one example of using Android Architecture Components Paging along with both Firestore and Realtime Database.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441