Answer the question
In order to leave comments, you need to log in
Why is the RecyclerView list not updating?
Stuck on chapter 17 of Android programming (Big nerd ranch). Help me figure out what I'm doing wrong.
I did the Callbacks interface with the update method of another fragment as it should be in the detail fragment class. In the reference type variable of the mCallbacks interface, I get my HostActivity (using the onAttach() override ), in which I have an implementation of this method that triggers the RecyclerView update .
public void onCrimeUpdated(Crime crime) {
CrimeListFragment listFragment = (CrimeListFragment)
getSupportFragmentManager()
.findFragmentById(R.id.fragment_container);
listFragment.updateUI();
}
public 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();
}
}
private void updateCrime(){
CrimeLab.get(getActivity()).updateCrime(mCrime);
mCallbacks.onCrimeUpdated(mCrime);
}
public void updateCrime(Crime crime){
String uuidString = crime.getId().toString();
ContentValues values = getContentValues(crime);
mDatabase.update(CrimeTable.NAME, values,
CrimeTable.Cols.UUID + " = ?",
new String[] { uuidString });
}
Answer the question
In order to leave comments, you need to log in
In general, your question does not correlate with the topic.
I understand that all callbacks work for you and are called (look at debugging, if you're not sure). If not, here's a first-hand manual for you - everything is elementary there https://developer.android.com/training/basics/frag...
Your question is why changes are not displayed.
So. updateUi method. If you don't have an adapter (probably when creating an activity, or turning it - that is, again when creating it), the resulting list of objects is taken from you and shoved into the adapter - all the rules.
But if there is an adapter, as Denis Zagaevsky already said, you have nothing to do. Yes, you told the adapter that you changed the data, but you didn't - you didn't pass a new list of objects to the adapter. You must first push a new list into the adapter and modify the existing one, and then call notifyDataSetChanged.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question