Firestore 分页最后一个可见文档抛出越界异常



尝试使用这些链接使用 Firestore 分页 https://stackoverflow.com/a/50742175/4232013 但是在集合中添加了一些文档后,我突然面临 ArrayIndexOutOfBoundsException 任何人都可以帮助我指出我在哪里犯了错误谢谢。 我的代码:

private int limit = 2;
private int PostLimit = 1;
private void GettingFollowingList() {
followingList = new ArrayList<>();
String UserID = FirebaseAuth.getInstance().getUid();
followingList.add(UserID);
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("Users")
.document(UserID)
.collection("UserDetails")
.document(UserID)
.collection("Following")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot documentSnapshot : task.getResult()) {
Followers followers = documentSnapshot.toObject(Followers.class);
followingList.add(followers.getFollow_id());
Log.d(TAG, "onComplete: FolloIDS: " + followers.getFollow_id());
}
ReadingPosts();

} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
}

我的寻呼 :

private void ReadingPosts() {

String userId = FirebaseAuth.getInstance().getUid();
FirebaseFirestore db = FirebaseFirestore.getInstance();
followingList.add(userId);
final CollectionReference UserRef = db.collection("UserPosts");
Query query = UserRef.limit(limit);
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull final Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot documentSnapshot : task.getResult()) {

for (String id : followingList) {

if (documentSnapshot.getId().equals(id)) {
Log.d(TAG, "onComplete: IDS3 :" + id);
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference PostRef = db.collection("UserPosts")
.document(id)
.collection("Posts");
Query query = PostRef.orderBy("timestamp", Query.Direction.DESCENDING).limit(PostLimit);
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot documentSnapshot1 : task.getResult()) {
Posts posts = documentSnapshot1.toObject(Posts.class);
postsList.add(posts);

}
}
postsAdapter.notifyDataSetChanged();
swipeRefreshLayout.setRefreshing(false);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "onFailure: Failed to get the posts");
}
});
}
}
postsAdapter.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;
}
}
@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 = UserRef.startAfter(lastVisible).limit(2);
nextQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task1) {
if (task1.isSuccessful()) {
for (QueryDocumentSnapshot documentSnapshot : task1.getResult()) {

for (String id : followingList) {

if (documentSnapshot.getId().equals(id)) {
Log.d(TAG, "onComplete: IDS3 :" + id);
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference PostRef = db.collection("UserPosts")
.document(id)
.collection("Posts");
Query query = PostRef.orderBy("timestamp", Query.Direction.DESCENDING).limit(PostLimit);
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot documentSnapshot1 : task.getResult()) {
Posts posts = documentSnapshot1.toObject(Posts.class);
postsList.add(posts);
}
}
postsAdapter.notifyDataSetChanged();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "onFailure: Failed to get the posts");
}
});
}
}

}
postsAdapter.notifyDataSetChanged();
lastVisible = task1.getResult().getDocuments().get(task1.getResult().size() - 1);
if (task1.getResult().size() < limit) {
isLastItemReached = true;
}
}
}
});
}
}
};
post_recycler_view.addOnScrollListener(onScrollListener);

} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
}

我在此行上面临错误输出

lastVisible = task1.getResult().getDocuments().get(task1.getResult().size() - 1);

我的日志输出

java.lang.ArrayIndexOutOfBoundsException: length=0; index=-1 at java.util.ArrayList.get(ArrayList.java:439(

错误消息告诉您获得的结果页面包含 0 个文档。 如果task1.getResult().size()返回 0,则从中减去 1 将是 -1,并且您不能索引为 -1 的数组。 在对它执行任何操作之前,您应该检查大小。

相关内容

  • 没有找到相关文章

最新更新