The following is the first few sections of a chapter from Exploring Android, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


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’s what we will work on in this tutorial.

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

Step #1: Injecting Our RosterRowHolder

Somewhere along the line, we need to register an OnCheckedChangedListener on the CheckBox in each row. Since we are using the data binding framework, we can let it do that registration, passing control to us when the user checks (or unchecks) the CheckBox.

In theory, we could have a data binding expression in the layout that directly updates the model. After all, the ToDoModel is what we are binding into the layout, via the model variable. In fact, the data binding framework has support for this, through what is known as two-way data binding. However, ToDoModel is immutable, so two-way data binding is not an option for us.

Instead, we can add another variable: our RosterRowHolder.

To do that, add another <variable> element to res/layout/todo_row.xml:

    <variable
      name="holder"
      type="com.commonsware.todo.RosterRowHolder" />

Then, modify bind() on RosterRowHolder to call setHolder() on the TodoRowBinding:

  void bind(ToDoModel model) {
    binding.setModel(model);
    binding.setHolder(this);
    binding.executePendingBindings();
  }

setHolder() will show up in red until Android Studio gets around to realizing that the data binding framework generated the method. This may require you to close and reopen the project.

Now we can start using the holder variable as needed.

Step #2: Binding to the Checked Event

The preview of this section will not appear here for a while, due to a time machine mishap.

Step #3: Passing the Event Up the Chain

The preview of this section was eaten by a grue.

Step #4: Saving the Change

The preview of this section was lost in the sofa cushions.

What We Changed

The preview of this section is in an invisible, microscopic font.