1

I am trying to implement pagination in my app I have a lot of data in my Firestore and I want to implement pagination with a progress bar. I have read many documents but all are confusing and most of in Kotlin, I need them in java. Please guide me.

I found the following guide by Alex Memo Sir How to paginate Firestore with Android?

but there are some doubts, one is what is the limit in it I am using it and my data is not showing after 15 items it is stopping??

COMPLETE CODE FOR IT

   firebaseFirestore.collectionGroup("room_details").orderBy("title", Query.Direction.ASCENDING).limit(limit)
            .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {


            progressBar.setVisibility(View.VISIBLE);
            if (task.isSuccessful()) {
                for (DocumentSnapshot d : task.getResult()) {
                    RoomsDetails obj = d.toObject(RoomsDetails.class);
                    roomsDetails.add(obj);
                }

                roomsAdapter.notifyDataSetChanged();
            }
            lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);

            RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                        isScrolling = true;
                        progressBar.setVisibility(View.GONE);
                    }
                }

                @Override
                public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) recyclerView.getLayoutManager());
                    int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
                    int visibleItemCount = linearLayoutManager.getChildCount();
                    int totalItemCount = linearLayoutManager.getItemCount();

                    if (isScrolling && (firstVisibleItemPosition + visibleItemCount == totalItemCount) && !isLastItemReached) {
                        isScrolling = false;
                        Query nextQuery = firebaseFirestore.collectionGroup("room_details").orderBy("title", Query.Direction.ASCENDING).startAfter(lastVisible).limit(limit);
                        nextQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                            @Override
                            public void onComplete(@NonNull Task<QuerySnapshot> t) {
                                if (t.isSuccessful()) {
                                    for (DocumentSnapshot d : t.getResult()) {
                                        RoomsDetails productModel = d.toObject(RoomsDetails.class);
                                        roomsDetails.add(productModel);
                                    }
                                    roomsAdapter.notifyDataSetChanged();
                                    lastVisible = t.getResult().getDocuments().get(t.getResult().size() - 1);

                                    if (t.getResult().size() < limit) {
                                        isLastItemReached = true;
                                    }
                                }
                            }
                        });
                    }
                }
            };

            recyclerViewRooms.addOnScrollListener(onScrollListener);


        }
    });
}

I have two doubts sir one is I want to add a Progress bar like this sir how can I implement it?? and second is what is limit() means here(on the first line) enter image description here

IT IS THROWING AN ERROR SIR HERE

    firebaseFirestore = FirebaseFirestore.getInstance();
       //HERE SIR COLLECTION REFENCE IS GIVING ME ERROR SAYING ClassCastException: com.google.firebase.firestore.Query cannot be cast to com.google.firebase.firestore.CollectionReference
        CollectionReference productsRef =  firebaseFirestore.collectionGroup("room_details");
    Query query = productsRef.orderBy("title", Query.Direction.ASCENDING).limit(limit);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I provided that answer, and yes, it's a working solution. Besides that, "it is stopping" doesn't provide enough information so we can help. If the app crashes, there is a stack trace. Please look that up on logcat, and add it to your question. – Alex Mamo Sep 26 '22 at 15:20
  • @Alex Mamo sir it is working but I have some doubt about what the limit() means here and second I want to implement the progress bar(i have added the image) please check sir –  Sep 26 '22 at 17:32
  • The limit that is set in order to paginate the results, it's **15**. To implement a ProgressBar you need to display it when you perform the request and hide it, once you got the data. – Alex Mamo Sep 26 '22 at 17:37
  • @AlexMamo so if I set my limit to 5 or 6 it mean it will load 6 items first?? sir I set the limit 5 and then it just showing me only 5 items in recyclerview sir –  Sep 27 '22 at 04:20
  • Yes, it will load 5 and then other 5 elements, and so on until you reach the end. – Alex Mamo Sep 27 '22 at 05:32
  • but sir in my case it is not loading after five i am trying to scroll no data is coming –  Sep 27 '22 at 05:40
  • my code is same as i have added above sir please check –  Sep 27 '22 at 05:41
  • Most likely it is not the same, since it doesn't work. – Alex Mamo Sep 27 '22 at 05:42
  • so what is the problem sir –  Sep 27 '22 at 05:43
  • StackOverflow is an inefficient interactive debugger. So y ou should debug the issue your self. – Alex Mamo Sep 27 '22 at 05:55
  • @AlexMamo sir it is throwing an error please check i have added above –  Sep 27 '22 at 06:14
  • @AlexMamo sir i have implemented this same code in my dummy app (only collection) where it is working fine but in my nested collection app this is not working –  Sep 27 '22 at 07:05

1 Answers1

1

According to your last edit, you are getting the following error:

ClassCastException: com.google.firebase.firestore.Query cannot be cast to com.google.firebase.firestore.CollectionReference

Because you are trying to save an object of type Query into a variable of type CollectionReference at the following line of code:

CollectionReference productsRef =  firebaseFirestore.collectionGroup("room_details");

Which is actually not possible in Java. Why? Because the Query class, doesn't extend the CollectionReference, it's exactly vice versa, hence the error. Please note FirebaseFirestore#collectionGroup(String collectionId) method, returns an object of type Query and not CollectionReference. So to solve this, please change the above line of code to:

Query productsRef =  firebaseFirestore.collectionGroup("room_details");
//

And your error will go away.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • thank you sir error gone but still problem –  Sep 27 '22 at 07:06
  • 1
    It's good to hear that the error is gone. However, if you have now another question that derives from this one, then it's considered a new question and should be added separately. So please post a new question, here on StackOverflow, using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Sep 27 '22 at 07:20