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.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.fragment_notice, container, false);
    recyclerView = view.findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    FirebaseRecyclerOptions<Notice> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<Notice>().setQuery(FirebaseDatabase.getInstance().getReference().child("Notice").child(Values.schoolCode).child(Values.student.getClass_txt()), Notice.class).build();

    recyclerViewAdapter = new RecyclerViewAdapter(firebaseRecyclerOptions);
    recyclerView.setAdapter(recyclerViewAdapter);

    if (recyclerViewAdapter.getItemCount() == 0) {
        Toast.makeText(getContext(), "No Notice Found", Toast.LENGTH_SHORT).show();
    }

    return view;
}

@Override
public void onStart() {
    super.onStart();
    recyclerViewAdapter.startListening();
}

@Override
public void onStop() {
    super.onStop();
    recyclerViewAdapter.stopListening();
}

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

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

   return inflater.inflate(R.layout.fragment_notice, container, false);
}

@Override
public onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
recyclerView = view.findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    FirebaseRecyclerOptions<Notice> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<Notice>().setQuery(FirebaseDatabase.getInstance().getReference().child("Notice").child(Values.schoolCode).child(Values.student.getClass_txt()), Notice.class).build();

    recyclerViewAdapter = new RecyclerViewAdapter(firebaseRecyclerOptions);
    recyclerView.setAdapter(recyclerViewAdapter);
}

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