getItemCount() on Adapter is returning 0
Asked Answered
R

3

5

I have created an adapter which is working fine. Now, I need to get a count of the no of data items in the adapter. i am using the function getItemcount() ,but I am getting a 0 every time. ALso, I am using Firebase for handling my database.

  final Firebase mRoot = new Firebase(FIREBASE_URL);
  mPosts= mRoot.child("posts");
  rvPosts.setHasFixedSize(true); //for performance improvement
    rvPosts.setLayoutManager(new LinearLayoutManager(this));    //for vertical list

   postAdapter = new FirebaseRecyclerAdapter<Hello, PostViewHolder>(Hello.class, R.layout.view_hello, PostViewHolder.class, mPosts.orderByChild("postedBy")) {

            @Override
            protected void populateViewHolder(final PostViewHolder postViewHolder, final Hello hello, int i) {
              //do something
         }
        };
        rvPosts.setAdapter(postAdapter);
        int postForSelect = postAdapter.getItemCount();

What can be the problem?

Rauch answered 21/6, 2016 at 6:59 Comment(3)
Where you have set the list to adapter .?? can i know what is mPost ..??Rossiter
@Rossiter I have edited the question. Kindly have a look.Rauch
Hey! I am following a tutorial and they use a certain version of realm. When I tried to apply everything they did my getItemCount() returned zero. I found out it was due to the version of realm. So maybe you need to check it out.Stein
P
9

The FirebaseRecyclerAdapter asynchronously synchronizes data from the database to your app. When you print the number of items, none of the data has been synchronized yet.

To know whenever the data in an adapter changes, you can register an AdapterDataObserver. For example in our Zero to App talk at I/O, we used this snippet to ensure the newest chat message would always be visible:

    adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
        public void onItemRangeInserted(int positionStart, int itemCount) {
            messagesList.smoothScrollToPosition(adapter.getItemCount());
        }
    });

I highly recommend you check out the full code for that minimal app.

Purge answered 21/6, 2016 at 14:14 Comment(1)
I'm using FirebaseIndexRecyclerAdapter and facing a related issu, can you please take a look: #44091790 ThanksLagging
P
0

Please check yout getItemCountMethod and refer that to the list's size you are using in Adapter

   @Override
public int getItemCount() {
    return list.size();
}
Pacha answered 21/6, 2016 at 7:2 Comment(6)
getItemCount() is an in-built fiunction.Rauch
Also, if I implement this method, what should be written in place of list?Rauch
you nedd to override it and in place of list add your arraylist of which you are using to display in your adapterPacha
Maybe it is a simple array, then try .length ?Bismuthic
where did to set the list?? In your adapter ??Pacha
There is not List in FirebaseRecyclerAdapter, only a Reference to the List hosted on Firebase DatabaseBelldas
R
0

I had trouble with the registerAdapterDataObserver method as it caused flickering when I tried to use it to manage views. However, I found another path:

Since you're already using a Firebase database, you can handle this by directly checking if your database list is empty or not. You can do this as a live snapshot listener or as a simple get query (for one-time reading). For example, in your Activity or Fragment (If you're using Firestore):

private CollectionReference myCollectionRef = db.collection("users");

myCollectionRef.addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot value,
                                @Nullable FirebaseFirestoreException e) {
                if (e != null) {
                    Log.w(TAG, "Listen failed.", e);
                    return;
                }

            assert value != null;
            if (value.getDocuments().size() > 0) { // List is populated

            } else { // List is empty
                //TODO: YOUR WORK HERE
            }
        }
    });
Righthand answered 6/4, 2020 at 12:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.