How to get the checked items from a multi choice item in a Alert.Builder?
Is there a way to get some infomation about the checked items in the next Alert.Builder? I need to save some booleans in SharedPreferences when somebody cl开发者_StackOverflow中文版icked the positive button. Those booleans are from the choices the user made in the alert. How can I get them?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final SharedPreferences preferences = getSharedPreferences("type_settings", MODE_PRIVATE);
boolean[] selectedTypes = getSelectedTypes(preferences);
builder.setIcon(R.drawable.menu_type)
.setTitle(R.string.list_dialog_title)
.setMultiChoiceItems(R.array.select_type_items, selectedTypes,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) {
}
})
.setPositiveButton(R.string.types_save, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
SharedPreferences.Editor prefEditor = preferences.edit();
}
})
.setNegativeButton(R.string.types_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
})
.create();
builder.show();
You could use getCheckedItemIds()
or getCheckedItemPositions ()
to get a list of checked items from the listview. You should use AlertDialog.getListView()
to get the dialog's listview first.
mAlert.getListView().getCheckedItemPositions ();
I managed to resolve the problem in some way, but i don't know if it's a good practice. Here is the code:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final SharedPreferences preferences = getSharedPreferences("type_settings", MODE_PRIVATE);
final String[] availableTypes = getResources().getStringArray(R.array.select_type_items);
final boolean[] selectedTypes = getSelectedTypes(preferences, availableTypes);
builder.setIcon(R.drawable.menu_type)
.setTitle(R.string.list_dialog_title)
.setMultiChoiceItems(R.array.select_type_items, selectedTypes,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) {
selectedTypes[whichButton] = isChecked;
}
})
.setPositiveButton(R.string.types_save, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
SharedPreferences.Editor prefEditor = preferences.edit();
saveSelectedTypes(prefEditor, availableTypes, selectedTypes);
}
})
.setNegativeButton(R.string.types_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
})
.create();
builder.show();
For storage efficiency, you could convert the boolean array to an integer before storing it as a preference. But beware, the boolean array that initializes the AlertDialog list has the reverse order of the boolean array results from the AlertDialog. (Seems to be a consistent reversal: happens on OS 2.3.6 and 4.2.2 ... I haven't checked any other versions.)
精彩评论