Issue
So in my app, I'm using Firestore Recyclerview and I want to count the items showed in it. I've tried using registerAdapterDataObserver and onItemRangeInserted and they seem to work ONLY if there is at least one item in the RecyclerView
. If there is none it doesn't even run.
Then, I've tried to implement the getItemCount() method but it always returns 0.
I have a class which is the adapter for the FirestoreRecyclerView.
private OnItemClickListener listener;
public JobsAdapter(@NonNull FirestoreRecyclerOptions<Job> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull jobHolder holder, int position, @NonNull Job model) {
holder.textViewTitle.setText(model.getJobName());
holder.textViewDescription.setText(model.getJobDescription());
holder.textViewPostDate.setText(model.getJobDate());
}
@Override
public int getItemCount() {
Log.d("adaptercount", "getItemCount: " + super.getItemCount());
return super.getItemCount();
}
@NonNull
@Override
public jobHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.job_item, parent, false);
return new jobHolder(v);
}
class jobHolder extends RecyclerView.ViewHolder {
TextView textViewTitle;
TextView textViewDescription;
TextView textViewPostDate;
public jobHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.text_view_title);
textViewDescription = itemView.findViewById(R.id.text_view_desc);
textViewPostDate = itemView.findViewById(R.id.text_view_post_date);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION && listener != null) {
listener.onItemClick(getSnapshots().getSnapshot(position), position);
}
}
});
}
}
Here the getItemCount() method works perfectly, but when I try to call this method in another fragment it returns 0.
My RecyclerViewInit() method in the fragment:
Query query = prevJobRef.whereEqualTo("jobIsClosed", false).whereEqualTo("jobSenderID", userID);
FirestoreRecyclerOptions<Job> options = new FirestoreRecyclerOptions.Builder<Job>().
setQuery(query, Job.class)
.build();
adapter = new JobsAdapter(options);
RecyclerView recyclerView = view.findViewById(R.id.recycler_view_activeJobs);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
public void onItemRangeInserted(int positionStart, int itemCount) {
Log.d("count", String.valueOf(adapter.getItemCount()));
}
});
Log.d("count2", String.valueOf(adapter.getItemCount()));
}
I have a feeling that it's an amateur mistake and I hope you guys can help me out!
Solution
I've tried using registerAdapterDataObserver and onItemRangeInserted and they seem to work ONLY if there is at least one item in the
RecyclerView
. If there is none it doesn't even run.
That's the expected behavior since onItemRangeInserted()
methods fires only when at least one item is inserted in the adapter. If there is no item to be inserted, the onItemRangeInserted()
method will never fire. However, there are two ways in which you can solve this problem. The first one would be to override the onChanged()
method in the same way you did onItemRangeInserted()
, as it will be triggered even if there no items. And the second option that you have is to override the onDataChanged()
method directly in your adapter class, as explained in my answer from the following post:
The second option is possible only because you are using the Firebase-UI library.
Answered By - Alex Mamo
Answer Checked By - Gilberto Lyons (JavaFixing Admin)