Reputation: 69
My recyclerview is not reaching onCreateViewHolder or getItemCount. I have checked and am sure that my item count isn't 0, I have added breakpoints in the adapter but except the constructor no other method is being called.
Questions I have looked at:
RecyclerView.Adapter:
// I have added only the required methods.
public class JournalAdapter extends RecyclerView.Adapter<JournalAdapter.ViewHolder> {
public JournalAdapter(Context context, List<Map<String, Object>> diaryListMaps) {
this.mInflater = LayoutInflater.from(context);
this.diaryListMaps = diaryListMaps;
Log.d(TAG, "Adapter Size: " + diaryListMaps.size());
Log.d(TAG, String.valueOf(diaryListMaps));
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.jourrnal_card, parent, false);
JournalAdapter.ViewHolder viewHolder = new JournalAdapter.ViewHolder(view);
Log.d(TAG, "onCreateViewHolder");
return viewHolder;
}
@Override
public int getItemCount() {
if (diaryListMaps.size() == 0) {
Log.d(TAG, "getItem: " + 1);
return 1;
}
else {
Log.d(TAG, "getItem: " + 10);
return diaryListMaps.size();
}
}
}
Fragment:
mViewModel = ViewModelProviders.of(this).get(DiaryDashboardViewModel.class);
mViewModel.getJournals().observe(getViewLifecycleOwner(), journalList -> {
Log.d(TAG, String.valueOf(journalList));
diaryListMaps.addAll(journalList);
journalAdapter = new JournalAdapter(getContext(), diaryListMaps);
});
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(RecyclerView.VERTICAL);
journalRecycler.setHasFixedSize(true);
journalRecycler.setLayoutManager(layoutManager);
journalRecycler.setAdapter(journalAdapter);
getJournals
public MutableLiveData<List<Map<String, Object>>> getJournals() {
Log.d(TAG, "getJournal");
dailyWeekColRef
.get()
.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
List<Map<String, Object>> dummyList = new ArrayList<>();
for (QueryDocumentSnapshot documentSnapshot: Objects.requireNonNull(task.getResult())) {
Map<String, Object> dummyMap = documentSnapshot.getData();
Log.d(TAG, "Retrived data " + dummyMap);
dummyList.add(dummyMap);
}
journal.postValue(dummyList);
}
else {
Log.d(TAG, "task unsuccessful " + task);
}
});
return journal;
}
Upvotes: 0
Views: 178
Reputation: 6089
Please call new JournalAdapter(getContext(), diaryListMaps);
before setAdapter
method is call and notify when your observer get the value
mViewModel.getJournals().observe(getViewLifecycleOwner(), journalList -> {
Log.d(TAG, String.valueOf(journalList));
diaryListMaps.clear();
diaryListMaps.addAll(journalList);
journalAdapter.notifyDataSetChanged();
});
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(RecyclerView.VERTICAL);
journalRecycler.setHasFixedSize(true);
journalRecycler.setLayoutManager(layoutManager);
journalAdapter = new JournalAdapter(getContext(), diaryListMaps);
journalRecycler.setAdapter(journalAdapter);
Upvotes: 0
Reputation: 10254
Try this
mViewModel.getJournals().observe(getViewLifecycleOwner(), journalList -> {
Log.d(TAG, String.valueOf(journalList));
if(hournalAdapter==null){
journalAdapter = new JournalAdapter(getContext(), diaryListMaps);
LinearLayoutManager layoutManager = new
LinearLayoutManager(getContext(),RecyclerView.VERTICAL,false);
journalRecycler.setLayoutManager(layoutManager);
journalRecycler.setAdapter(journalAdapter);
} else {
diaryListMaps.clear();
diaryListMaps.addAll(journalList);
journalAdapter.notifyDataSetChanged();
}});
Also change
@Override
public int getItemCount() {
return diaryListMaps.size();
}
The issue with your code is that whenever there is a change in the data, adapter will get initiated again and again, which is not the right way to handle it.
You can check whether the adapter is null or not on the observe method, if it is not null then simply update the list and call notifyDataSetChanged
Upvotes: 0