分页时越界异常

时间:2019-06-04 10:58:02

标签: android pagination google-cloud-firestore

我在https://www.youtube.com/watch?v=KdgKvLll07s&t=118s处实现了Alex Mamo提供的解决方案。但是,我不断收到此错误:

java.lang.ArrayIndexOutOfBoundsException: length=0; index=-1

在此行的onScrollListener内:

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

这是oncreateView方法中的主要代码段:

restaurantItemsArrayList = new ArrayList<>();
        restaurantRecycler = restaurantView.findViewById(R.id.restaurant_recycler);
        linearLayoutManager = new LinearLayoutManager(restaurantContext);
        restaurantRecycler.setHasFixedSize(true);
        restaurantRecycler.setLayoutManager(linearLayoutManager);

        restaurantQuery = FirebaseFirestore.getInstance().collection("Restaurant_Data").whereEqualTo("restaurantAddress", selectedCity).limit(LOAD_LIMIT);
        restaurantQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (DocumentSnapshot document : task.getResult()) {
                        RestaurantItems restaurantItems = document.toObject(RestaurantItems.class);
                        restaurantItemsArrayList.add(restaurantItems);
                    }
                    restaurantAdapter = new RestaurantAdapter(restaurantContext, restaurantItemsArrayList);
                    restaurantRecycler.setAdapter(restaurantAdapter);
                    lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);

                    restaurantAdapter.setRestaurantAdapterListener(new RestaurantAdapter.RestaurantAdapterListener() {
                        @Override
                        public void onRestaurantSend(String restaurantId, String menuPath) {
                            String path = menuPath+"/Menu_Card";
                            bundle.putString("restaurantId", restaurantId);
                            bundle.putString("restaurantPath", path);
                            bundle.putBoolean("direction", true);
                            restaurantSelectListener.onRestaurantSelect(bundle);
                        }
                    });

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

                        @Override
                        public void onScrolled(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) && !isLastItem) {
                                isScrolling = false;

                                restaurantNextQuery = FirebaseFirestore.getInstance().collection("Restaurant_Data").whereEqualTo("restaurantAddress", selectedCity).startAfter(lastVisible).limit(LOAD_LIMIT);
                                restaurantNextQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                    @Override
                                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                        if (task.isSuccessful()) {
                                            for (DocumentSnapshot documentSnapshot : task.getResult()) {
                                                RestaurantItems restaurantItems = documentSnapshot.toObject(RestaurantItems.class);
                                                restaurantItemsArrayList.add(restaurantItems);
                                            }
                                            restaurantAdapter.notifyDataSetChanged();
                                            lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);

                                            restaurantAdapter.setRestaurantAdapterListener(new RestaurantAdapter.RestaurantAdapterListener() {
                                                @Override
                                                public void onRestaurantSend(String restaurantId, String menuPath) {
                                                    String path = menuPath+"/Menu_Card";
                                                    bundle.putString("restaurantId", restaurantId);
                                                    bundle.putString("restaurantPath", path);
                                                    bundle.putBoolean("direction", true);
                                                    restaurantSelectListener.onRestaurantSelect(bundle);
                                                }
                                            });

                                            if (task.getResult().size() < LOAD_LIMIT) {
                                                isLastItem = true;
                                            }
                                        }
                                    }
                                });
                            }
                        }
                    };
                    restaurantRecycler.addOnScrollListener(onScrollListener);
                }
            }
        });

        backArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bundle.putBoolean("direction", false);
                restaurantSelectListener.onRestaurantSelect(bundle);
            }
        });
        return restaurantView;
    }

1 个答案:

答案 0 :(得分:0)

您需要使用下面的尺寸检查验证将行换行,如下所示。因为如果没有找到文档的记录,那么查询也会返回0条记录的成功响应。

if(task.getResult().size() > 0) {
  lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
}