Solution 1 :
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);
}
Problem :
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! 🙂
Comments
Comment posted by onDataChanged
The data retrieval is going to be asynchronous, so the short answer is “no, you can’t get the item count before returning from onCreateView”. If you want to check the size after it gets the data, you may be able to do so in
Comment posted by Aayush Shukla
@TylerV Thanks for the comment, I got your point, and did it on the
Comment posted by Tyler V
While splitting it like this is recommended, the view definitely exists once you’ve called
Comment posted by Ihor Bykov
Yeah, you are right. The view will be created after inflate call.