0

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;
    }
Mood Board
  • 59
  • 3
  • 7

2 Answers2

0

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);
Mittal Varsani
  • 5,601
  • 2
  • 15
  • 27
  • Gives an IndexOutOfBoundsException in the onBindViewHolder method – Mood Board Jul 20 '20 at 05:33
  • It doesn't work. I am using `getJournals()` some where else as well and it is working perfectly fine there. So most likely, the observer is not working and thus the fragment is passing a null list. Do you have a solution for that? – Mood Board Jul 20 '20 at 08:19
  • Can you edit the question with the code of getJournals()? – Mittal Varsani Jul 20 '20 at 08:34
0

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

Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77