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 clicked 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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With