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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
TeeKay
  • 1,025
  • 2
  • 22
  • 60
  • Where you have set the list to adapter .?? can i know what is mPost ..?? – Moinkhan Jun 21 '16 at 07:06
  • @Moinkhan I have edited the question. Kindly have a look. – TeeKay Jun 21 '16 at 07:12
  • 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. – M K Jul 10 '17 at 10:02

3 Answers3

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I'm using FirebaseIndexRecyclerAdapter and facing a related issu, can you please take a look: http://stackoverflow.com/questions/44091290/firebaseindexrecycleradapter-ondatachanged-is-called-but-getitemcount-always-r Thanks – Shirane85 May 20 '17 at 22:13
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();
}
Tanveer Bulsari
  • 213
  • 1
  • 10
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
            }
        }
    });
The Fluffy T Rex
  • 430
  • 7
  • 22