-
Notifications
You must be signed in to change notification settings - Fork 29
06 Use the dialog to show list items
A dialog, which is created by the class MaterialDialog.Builder
can also be used to show list items. Three different choice modes are available. Each one of them will be discussed in one of the following subsections.
To show list views within the dialog using no choice mode, which means that the items are not selectable, the following methods can be used:
setItems(CharSequence[], OnClickListener):Builder
setItems(int, OnClickListener):Builder
setAdapter(ListAdapter, OnClickListener):Builder
The setItems
-methods mentioned above can be used to specify the list items as an array of CharSequence
objects or by passing the resource id of an array resource. When using these methods, the builder will create an adapter to manage the items by itself. Alternatively, the setAdapter
-method allows to pass such an adapter to the builder. Such an adapter must take care of storing and displaying the individual items. Furthermore, all of the methods shown above allows to pass a listener, which has to implement the interface DialogInterface.OnClickListener
, to the builder. Such a listener will be notified, when the user clicks one of the list items. Clicking a list item, when using no choice mode, will cause the dialog to be closed. The following screenshot shows a dialog, which contains list views using no choice mode:
To show selectable list items, of whom only one item can be selected at once, the following methods can be used:
setSingleChoiceItems(CharSequence[], int, OnClickListener):Builder
setSingleChoiceItems(int, int, OnClickListener):Builder
setSingleChoiceItems(ListAdapter, int, OnClickListener):Builder
These methods are very similar to the methods discussed in the previous section, but all of them expect an additional Integer value, which specifies the position of the item, which should be selected by default, as a parameter. If this value is set to -1, no item will be selected by default. The semantic of the other parameters is equal to the semantics of the previously discussed method parameters. When a list item is selected while using single choice mode, the dialog will not be closed. The following screenshot shows a dialog, which contains list views using the single choice mode:
The third possibility to show list items within the dialog is to use the multi choice mode, which means, that multiple items can be selected at once. This can be established by using one of the following methods:
setMultiChoiceItems(CharSequence[], boolean[], OnMultiChoiceClickListener):Builder
setMultiChoiceItems(int, boolean[], OnMultiChoiceClickListener):Builder
These methods are slightly different from the already discussed ones, as the listener, which can be passed to be notified, when a list item's selection changes, has to implement the interface DialogInterface.OnMultiChoiceClickListener
. Also, the items, which should be selected by default, are specified by passing a boolean array. The array's length must be equal to the number of items, as the boolean value at each index indicates, whether the list item at the corresponding position should be selected, or not. Similar to the single choice mode, the dialog is not closed when a list item is selected using the multi choice mode. The following screenshot shows a dialog, which contains list items using the multi choice mode: