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.
To provide a list of items in a dialog, we can specify the items in much the same way as a simple message:
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(); }
SetAdapter()
method:var adapter = new ArrayAdapter( this, Android.Resource.Layout.SimpleListItem1, items); dialog.SetAdapter(adapter, (s, e) => { var index = e.Which; });
SetSingleChoiceItems()
method:int selected = -1; dialog.SetSingleChoiceItems(items, selected, (s, e) => { selected = e.Which; });
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; });
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.
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.
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.