Selection alerts

Sometimes we have to ask the user to select one or more items from a larger collection. This may be necessary as normal dialogs only have up to three buttons, but a fully customized UI is unnecessary.

How to do it...

To provide a list of items in a dialog, we can specify the items in much the same way as a simple message:

  1. If we need to provide the user with a selection of items in a dialog, we can also use the AlertDialog instance and the SetItems() method:
    string[] items = new string[] { ... }
    using (var dialog = new AlertDialog.Builder(this)) {
      dialog.SetTitle("Alert Title");
      dialog.SetPositiveButton("Close", delegate { 
      });
      dialog.SetItems(items, (s, e) => {
        int index = e.Which;
      });
      dialog.Show();
    } 
  2. We can also use data adapters in a dialog, through the SetAdapter() method:
    var adapter = new ArrayAdapter(
      this, Android.Resource.Layout.SimpleListItem1, items);
    dialog.SetAdapter(adapter, (s, e) => {
      var index = e.Which;
    });
  3. If we want to support a single-select radio button list, we use the SetSingleChoiceItems() method:
    int selected = -1;
    dialog.SetSingleChoiceItems(items, selected, (s, e) => {
      selected = e.Which;
    });
  4. We use the SetMultiChoiceItems() method if we want to support a multi-select checklist:
    bool[] selected = new bool[items.Length]; 
    dialog.SetMultiChoiceItems(items, multiChecked, 
      (s, e) => {
        int index = e.Which;
        bool isChecked = e.IsChecked;
        selected[index] = isChecked;
      });

How it works...

Using a dialog to display a list is very useful if switching context is to be avoided. A list in a dialog provides great flexibility, such as providing an option to select multiple items or none at all.

There are three types of lists that can appear in a dialog. First, there is the traditional single-select dialog that will hide when an item is selected. Then, there is the radio list, which also allows the user to select a single item. This list differs from the traditional list in two ways: the dialog has to be dismissed manually, and the dialog allows us to specify which item is selected when it appears. Finally, there is the check list, which is very similar to the radio list but allows multiple items to be selected.

Note

The list view in a dialog can be populated from a string array, an adapter, a cursor, or a resource.

There are several ways to populate the list in the dialog. We could use an array resource, a string array, a ListAdapter instance, or a Cursor instance. The traditional list dialog uses the SetItems() method to specify an array or resource, the SetAdapter() method for a ListAdapter instance, and the SetCursor() method for a Cursor instance. All these methods allow us to provide the list as well as a delegate that will get invoked when the user selects an item.

If we want to use the radio list, we use the SetSingleChoiceItems() method. This method allows us to specify the item collection, the index of the initially selected item, and the delegate that will be invoked when an item is selected. If -1 is provided for the selected index, nothing will be selected initially.

If we want to use the check list, we use the SetMultiChoiceItems() method. We can specify the item collection, an array with the initially selected indexes, and the delegate that will be invoked when an item is checked or unchecked. The selected items are determined by a bool array, which will contain true at every index that should be selected. If null is provided instead of an array, nothing will be selected initially. The bool array must be the same size as the item collection.

Note

The list view in a dialog can be a single select or multiselect.

In all the methods, the delegate will receive an EventArgs method that will contain details about which item was selected. The EventArgs method contains a Which property representing the index of the item that was selected. Additionally, the SetMultiChoiceItems() method provides an extra property, IsChecked, which holds a value representing whether the item was checked or unchecked.

Note

The delegate is fired each time the user selects an item, not when the dialog is dismissed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset