Step #5: Getting Updated Items

The problem lies in RosterMotor. There, we have an items property, populated from the repository’s list of items:

  val items = repo.items

When we return to our RosterListFragment, we go through onCreateView() and onViewCreated() again. We ask the motor for the items and display them in the list. The problem is that we still have the same RosterMotor instance as we did originally, and it has the same items collection as we did originally. However, we replaced the items collection in the repository… but RosterMotor does not know about this.

Ideally, we would be using some sort of “reactive” system that allows RosterMotor to respond as soon as ToDoRepository has updated contents. We will switch to that sort of solution later in the book. For now, though, we can settle for simply getting the list from the repository every time.

With that in mind, replace the items property in RosterMotor with a nearly-identical getItems() function:

  fun getItems() = repo.items

Then, in RosterListFragment, change our submitList() call in onViewCreated() to use this new function, instead of the now-replaced items property:

    adapter.submitList(motor.getItems())

Now, when you save changes to an item, you return to the list, and the list will reflect the changes that you made.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.