How To Get Documents Id ? Firestore Paginate
I am using Firebase Firestore Database, I am trying to load data using dual query for pagination recyclerver data, using this Stackoverflow ans link My code is for recycerview to
Solution 1:
It could be because you are sending the position and you that gives you the error, try to send final String postid=task.getResult().getDocuments().get(position-1).getId();
and see if you are getting the object that you want.Let me know it that works for you
Solution 2:
finally i solved error. the error is size of next query querysnapshot
size. i use new List
for saving document ids using this method on both side in first query and next quesry
for (QueryDocumentSnapshot document : t.getResult()) {
listId.add(document.getId());
}
and in on Adapter
get id using listId
with position.
final query code is
Task<QuerySnapshot> mSnapy;
booleanisScrolling=false;
booleanisLastItemReached=false;
DocumentSnapshot lastVisible;
NewPostAdapter mNewPostAdapter;
privatevoidgetNewFireData() {
finalintlimit=3;
mRecyclerView = rootView.findViewById(R.id.rv_post_list);
final LinearLayoutManager mManeger=newLinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mManeger);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.invalidate();
final List<Posts> list = newArrayList<>();
final List<String> listId=newArrayList<>();
FirebaseFirestorerootRef= FirebaseFirestore.getInstance();
finalCollectionReferencepostRef= rootRef.collection("posts");
Queryquery= postRef.orderBy("datetime",
Query.Direction.DESCENDING).limit(limit);
query.get().addOnCompleteListener(newOnCompleteListener<QuerySnapshot>() {
@OverridepublicvoidonComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()){
lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
for (DocumentSnapshot document : task.getResult()) {
PostspostModel= document.toObject(Posts.class);
list.add(postModel);
}
for (QueryDocumentSnapshot document : task.getResult()) {
listId.add(document.getId());
}
mSnapy=task;
mNewPostAdapter = newNewPostAdapter(list,getActivity()
, mSnapy,listId,myContext);
mRecyclerView.addOnScrollListener(newRecyclerView.OnScrollListener() {
@OverridepublicvoidonScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(newState==AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL){
isScrolling =true;
}
}
@OverridepublicvoidonScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManagerlinearLayoutManager= ((LinearLayoutManager) recyclerView.getLayoutManager());
intfirstVisibleItemPosition= linearLayoutManager.findFirstVisibleItemPosition();
intvisibleItemCount= linearLayoutManager.getChildCount();
inttotalItemCount= linearLayoutManager.getItemCount();
if (isScrolling && (firstVisibleItemPosition + visibleItemCount ==
totalItemCount) && !isLastItemReached) {
isScrolling = false;
QuerynextQuery= postRef.orderBy("datetime",
Query.Direction.DESCENDING).startAfter(lastVisible).limit(limit);
nextQuery.get().addOnCompleteListener(newOnCompleteListener<QuerySnapshot>() {
@OverridepublicvoidonComplete(@NonNull Task<QuerySnapshot> t) {
if (t.isSuccessful()) {
for (DocumentSnapshot d : t.getResult()) {
PostsproductModel= d.toObject(Posts.class);
list.add(productModel);
}
for (QueryDocumentSnapshot document : t.getResult()) {
listId.add(document.getId());
}
mSnapy = t;
System.out.println("Task Data "+mSnapy.getResult().getDocuments());
mNewPostAdapter.notifyDataSetChanged();
lastVisible = t.getResult().getDocuments().get(t.getResult().size() - 1);
if (t.getResult().size() < limit) {
isLastItemReached = true;
}
}
}
});
}
}
});
// mNewPostAdapter.setHasStableIds(true);
mRecyclerView.setAdapter(mNewPostAdapter);
}
}
});
}
Solution 3:
I found another answer that code is below Just create a new obejct on Modal Post.class
dodId
and use it like below
for (QueryDocumentSnapshotdocument : task.getResult()) {
Posts modal=document.toObject(Posts.class);
modal.setDoc_id(document.getId());
list.add(modal);
}
Post a Comment for "How To Get Documents Id ? Firestore Paginate"