Step #6: Getting Control on Filter Choices

In particular, clicking on the submenu items does not even change their checked state. Even though our submenu looks like a group of radio buttons, it does not behave like one automatically. Instead, we need to add some code for that. Plus, we really ought to consider actually doing the filtering.

In RosterListFragment, replace the current onOptionsItemSelected() function with:

  override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
      R.id.add -> {
        add()
        return true
      }
      R.id.all -> {
        item.isChecked = true
        motor.load(FilterMode.ALL)
        return true
      }
      R.id.completed -> {
        item.isChecked = true
        motor.load(FilterMode.COMPLETED)
        return true
      }
      R.id.outstanding -> {
        item.isChecked = true
        motor.load(FilterMode.OUTSTANDING)
        return true
      }
    }

    return super.onOptionsItemSelected(item)
  }

onOptionsItemSelected() will get called when the user clicks on the checkable sub-menu items, so we add cases to our when for those three menu items. For each, we mark it as checked, so the radio button associated with that “checkable” menu item becomes checked (and others as unchecked). Plus, we call load() on our RosterMotor with the appropriate FilterMode for that menu item.

At this point, if you run the app, and you have some to-do items, the filtering should work:

However, there are a couple of minor UI glitches that we still need to fix, which we will handle in the remaining steps of this tutorial.


Prev Table of Contents Next

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