0

Below is the code I am using to filter the data. The Query passed to the method contains a whereEqualTo() clause to filter the data accordingly. To display the list I have to set the query to the adapter like this

adapter.setQuery(query1);

and then set the adapter to the recyclerView as this

recyclerView.setAdapter(adapter);

Till this code loads the documents accurately according to the limit. But now when the pagination has to be performed I again set the adapter with another query nextQuery which loads the new data but replaces the previously loaded data, but I want to add the new data to previously loaded data and not to replace the previously loaded data

How should I correct this? I want to achieve the pagination here. I am using Firestore as backend for my android application.

If any more information is required please let me know

The index for the firestore database had also been created.

CODE

public void Paginate1(Query query, Filters filters){

        Log.d("queryCheck", filters.toString() + "Hello 1");
        Query query1 = query.limit(5);



        query1.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {

                    adapter.setQuery(query1);

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

                    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) && !isLastItemReached) {
                                isScrolling = false;
                                Query nextQuery = query1.startAfter(lastVisible).limit(2);
                                nextQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                    @Override
                                    public void onComplete(@NonNull Task<QuerySnapshot> t) {
                                        if (t.isSuccessful()) {
                                            adapter.setQuery(nextQuery);
                                            adapter.notifyDataSetChanged();
                                            lastVisible = t.getResult().getDocuments().get(t.getResult().size() - 1);

                                            if (t.getResult().size() < 2) {
                                                isLastItemReached = true;
                                            }
                                        }
                                    }
                                });
                            }
                        }

                    };

                    recyclerView.addOnScrollListener(onScrollListener);
                    recyclerView.setAdapter(adapter);

                }
            }
        });
    }

SETQUERY in Adapter

public void setQuery(Query query) {
        // Stop listening
        stopListening();

        // Clear existing data
        mSnapshots.clear();
        notifyDataSetChanged();

        // Listen to new query
        mQuery = query;
        startListening();
    }
  • **[This](https://stackoverflow.com/questions/50741958/how-to-paginate-firestore-with-android)** is a recommended way in which you can paginate queries by combining query cursors with the limit() method. I also recommend you take a look at this **[video](https://www.youtube.com/watch?v=KdgKvLll07s)** for a better understanding. – Alex Mamo May 21 '20 at 09:21
  • @AlexMamo if you can see, I have implemented the same thing, but not successful. Please understand my question and help me. –  May 21 '20 at 09:25
  • @AlexMamo I know you are an expert in firebase and I am new to this, and learning many things, please help me with this project, I am stuck on this for past few weeks. –  May 23 '20 at 01:41

0 Answers0