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;
boolean isScrolling = false;
boolean isLastItemReached = false;
DocumentSnapshot lastVisible;
NewPostAdapter mNewPostAdapter;
private void getNewFireData() {
final int limit = 3;
mRecyclerView = rootView.findViewById(R.id.rv_post_list);
final LinearLayoutManager mManeger=new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mManeger);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.invalidate();
final List<Posts> list = new ArrayList<>();
final List<String> listId=new ArrayList<>();
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
final CollectionReference postRef = rootRef.collection("posts");
Query query = postRef.orderBy("datetime",
Query.Direction.DESCENDING).limit(limit);
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()){
lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
for (DocumentSnapshot document : task.getResult()) {
Posts postModel = document.toObject(Posts.class);
list.add(postModel);
}
for (QueryDocumentSnapshot document : task.getResult()) {
listId.add(document.getId());
}
mSnapy=task;
mNewPostAdapter = new NewPostAdapter(list,getActivity()
, mSnapy,listId,myContext);
mRecyclerView.addOnScrollListener(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;
}
}
@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 = postRef.orderBy("datetime",
Query.Direction.DESCENDING).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()) {
Posts productModel = 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 (QueryDocumentSnapshot document : 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"