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:
- The first parameter to the lambda expression is the
CheckBox
that was checked. We do not need this, so we use_
to indicate that this is an unused parameter. Similarly, the second parameter to the lambda expression is aBoolean
indicating the current state of theCheckBox
. We do not need that either (it is the opposite of the current state of our model), so we use_
as the parameter name for it as well. -
TODO()
is a Kotlin function that serves as a marker, indicating that we are not yet done with this logic.
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.