Challenge: Submitting updated list/LiveData

I’m stuck at this part:

You will also need to update CrimeListFragment to submit an updated crime list to the recycler view’s adapter, rather than reassigning the recycler view’s adapter to a new adapter object every time you want to update the UI. You can submit a new list by calling ListAdapter.submitList(MutableList?). Or you can set up LiveData and observe the changes.

  1. I try to call .submitList() on crimeRecyclerView.adapter but crimeRecyclerView.adapter has no such function. So on what do I call .submitList() on?

  2. I’m not quite clear on the LiveData version either. What exactly should I be observing?

Hey LeNDuyA, both scenarios would do the job.

With option one you would have to change your Adapter class’s super class from RecyclerViewAdapter to ListAdapter. The ListAdapter class has the submitList() method.

Based not the docs, the ListAdapter is just a wrapper to the AsyncListDiffer class which has the submitList() method. Also, the example uses LiveData as well to update the list with the new updated data (not a requirement but kool).

Now you don’t necessarily need to use submitList method to update the list. If you are using LiveData you can simply observe the changes, assign your old list reference to the new List from the LiveData and simply call notifyDataSetChange() within the onChange() method. The LiveData link has an example you can use as a reference.

I’ll leave you with a file from one of my earlier projects where I used option 2 to update my list of Data. FYI, it is by no means perfect and it is written in java. Also, I created a new Adapter in this case whenever the list is updated so you would have to figure out how to omit that detail.

I haven’t had a chance to look at the 4th edition yet. I read the 3rd edition and love it! Hopefully, I’m leading you in the right direction and not making matters worse. :slight_smile:

Good luck! :muscle:

1 Like

When you read a sentence like “submit an updated crime list to the recycler view’s adapter, rather than reassigning the recycler view’s adapter to a new adapter object every time you want to update the UI”, you realise that OO is, in fact, a load of cobblers.

And you realize you aren’t, in fact, answering the question.

1 Like

I did as follows (type cast to CrimeAdapter):

    private fun updateUI(crimes: List<Crime>) {
        (crimeRecyclerView.adapter as CrimeAdapter).submitList(crimes)
    }