A
A
Anatoly2018-03-07 19:03:11
Android
Anatoly, 2018-03-07 19:03:11

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();
    }

But the update occurs only after all fragments are redrawn (when, for example, I flip the device), although this uses the same updateUI () method that does not update the list if called from the mCallbacks variable.
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();
        }
    }

the onCrimeUpdated() method is fired from the fragment
private void updateCrime(){
        CrimeLab.get(getActivity()).updateCrime(mCrime);
        mCallbacks.onCrimeUpdated(mCrime);
    }

Actually updating the data:
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 });
    }

please help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mitaichik, 2018-03-07
@TonyWrong

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 question

Ask a Question

731 491 924 answers to any question