Step #13: Populating the Layout

Finally, we can use our ToDoModel to fill in the widgets of our layout.

Add this onViewCreated() function to DisplayFragment:

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    motor.getModel()?.let {
      binding?.apply {
        completed.visibility = if (it.isCompleted) View.VISIBLE else View.GONE
        desc.text = it.description
        createdOn.text = DateUtils.getRelativeDateTimeString(
          requireContext(),
          it.createdOn.toEpochMilli(),
          DateUtils.MINUTE_IN_MILLIS,
          DateUtils.WEEK_IN_MILLIS,
          0
        )
        notes.text = it.notes
      }
    }
  }

This retrieves the model given its ID and updates the widgets in the ToDoBinding based on that model:

At this point, if you run the app, and you click on one of the to-do items in the list, the full details should appear in the DisplayFragment:

ToDo Apps DisplayFragment
ToDo App’s DisplayFragment

Pressing BACK returns you to the list, as before.


Prev Table of Contents Next

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