Tracking the Completion Status

We have checkboxes in the list to show the completion status. However, the user can toggle these checkboxes. Right now, that is only affecting the UI — our models still have the old data. We should find out when the user toggles the checked state of a checkbox, then update the associated model to match. So, that is what we will work on in this tutorial.

This is a continuation of the work we did in the previous tutorial. The book’s GitLab repository contains the results of the previous tutorial as well as the results of completing the work in this tutorial.

Step #1: Registering for Events

We need to register an OnCheckedChangedListener on the CheckBox in each row, so we find out when that checkbox is checked and unchecked. We also will need to know which ToDoModel is bound to this row, so we can update the correct model.

To that end, modify bind() in RosterRowHolder to look like:

  fun bind(model: ToDoModel) {
    binding.apply {
      isCompleted.isChecked = model.isCompleted
      isCompleted.setOnCheckedChangeListener { _, _ -> TODO() }
      desc.text = model.description
    }
  }

We now have attached a lambda expression to our CheckBox check events. Of note:

If you were to run this and try checking or unchecking a CheckBox, your app will crash, as TODO() throws an exception as a way of getting the point across that you are not done yet.

(hence, do not run the app right now, unless you like crashing)


Prev Table of Contents Next

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