Issue
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)
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);
Solution
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.
Answered By - Alex Mamo
Answer Checked By - Pedro (JavaFixing Volunteer)