Geetika Sachdeva
Geetika Sachdeva

Reputation: 103

How to fetch data from firebase only 10 row at first time in recyclerview

In my android application i am fetching data from Firebase and displaying it in recyclerView. It is working fine but now i want that at first time only 10 rows should be fetched and when user scrolls recylerview then next 10 rows will be fetched. I am not understanding how to do it. I checked many question related this but couldn't link it with my code. Please check my code and tell me how can i do this.

arrayAdapter = new ContentViewRecyclerAdapter(contentname, this);
    recyclerView.setAdapter(arrayAdapter);

    String classname = getIntent().getStringExtra("classname");
    if(classname.equals("A")) {
       content = getIntent().getStringExtra("jokecontent");
       funnyJokesQuery = FirebaseDatabase.getInstance().getReference("ContentJokes").orderByChild("subcategory")
               .equalTo(content);
       funnyJokesQuery.addChildEventListener(new ChildEventListener() {
           @Override
           public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
               String contentdisplay = dataSnapshot.child("content").getValue(String.class);
               ContentModel contentModel = new ContentModel(contentdisplay);
               contentname.add(0,contentModel);
               arrayAdapter.notifyDataSetChanged();
               progressBar.setVisibility(View.GONE);
               arrayAdapter.notifyItemInserted(0);

           }
           @Override
           public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {


           }

           @Override
           public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

           }

           @Override
           public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

           }

           @Override
           public void onCancelled(@NonNull DatabaseError databaseError) {

           }
       });

    }

Upvotes: 0

Views: 1338

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

To get only 10 child nodes you can use limitToFirst(10) in your query:

funnyJokesQuery = FirebaseDatabase.getInstance().getReference("ContentJokes")
        .orderByChild("subcategory")
        .equalTo(content)
        .limitToFirst(10);
funnyJokesQuery.addChildEventListener(new ChildEventListener() {
    ...

To then get the next 10 items, you'll need to create a second query that starts at the last item of the previous page. This is because Firebase queries are based on cursors (a specific item) and not on offsets counts.

To track the anchor item, you could change your onChildAdded to:

       @Override
       public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
           ...

           lastCategorySeen = dataSnapshot.child("subcategory").getValue(String.class);
           lastKeySeen = dataSnapshot.getKey();
       }

And then the query for the next page look like:

FirebaseDatabase.getInstance().getReference("ContentJokes")
        .orderByChild("subcategory")
        .startAt(lastCategorySeen, lastKeySeen)
        .limitToFirst(11);

So the startAt here tells the database to start at the last item of the first page. We then retrieve 11 items, since the anchor item is present in both queries.

Note that this is a complex topic that has been covered extensively both on other sites and here. I recommend checking out some of the previous questions on firebase and pagination.

Upvotes: 3

Related Questions