This is our updateUI() method in Listing 10.9
private void updateUI() { CrimeLab crimeLab = CrimeLab.get(getActivity()); List<Crime> crimes = crimeLab.getCrimes(); if (mAdapter == null) { mAdapter = new CrimeAdapter(crimes); mCrimeRecyclerView.setAdapter(mAdapter); } else { mAdapter.notifyDataSetChanged(); } }
It seems like an odd way to implement things. Would there be a downside to just putting this part
CrimeLab crimeLab = CrimeLab.get(getActivity()); List<Crime> crimes = crimeLab.getCrimes(); mAdapter = new CrimeAdapter(crimes); mCrimeRecyclerView.setAdapter(mAdapter);
in the onCreateView() method, and then
mAdapter.notifyDataSetChanged();
in our onResume() method?
Combining them, getting a reference to a variable that may or may not be needed, and using an if-else statement feels like unnecessary complication to me, but I am also inexperienced so there’s probably something I’m not aware of that makes this the best implementation. If that’s the case, I’d like to learn!
Thanks!