Skip to content
Advertisement

recyclerViewAdapter.getItemCount() returning 0 even when it has objects inside?

I’m building a Fragment with a RecyclerView which displays notices retrieved from my database. Everything works fine, except for this line of code:

Problem:

recyclerViewAdapter.getItemCount()

This statement returns 0 even if the RecyclerViewAdapter has elements present. The full scenario is whenever the fragment loads, the Toast with the appropriate message pops up, but then after a slight delay, the first Notice appears from my database.

Question:

Is there a way with which the statement recyclerViewAdapter.getItemCount() gets called after all the fetching is done, as my interpretation says that the block gets executed as soon as the activity is created, not waiting for the adapter to confirm if there’s any Notice in the database or not?

Code Block:

Below is all the blocks of code which involve recyclerViewAdapter.

JavaScript

Note: I have initialized the variable itself in the global scope so as to Override the appropriate methods later in the code.

Thanks in advance! 🙂

Advertisement

Answer

There is no view created while you assign adapter to the recyclerView. My recommendation would be split logic between onCreateView and onViewCreated

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement