How to get marked items from an object with multiple choices in Alert.Builder?

Is there a way to get information about checked items in the next Alert.Builder? I need to store some boolean values ​​in SharedPreferences when someone pressed a positive button. These booleans are 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(); 
+4
source share
3 answers

I managed to somehow solve the problem, but I do not know if this is good. 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(); 
+2
source

You can use getCheckedItemIds() or getCheckedItemPositions () to get a list of checked items from the list. You must use AlertDialog.getListView() to view the list of dialogs first.

 mAlert.getListView().getCheckedItemPositions (); 
+9
source

For storage efficiency, you can convert a logical array to an integer before storing it as a preference. But be careful, the logical array that initializes the AlertDialog list has the reverse order of the results of the logical array from AlertDialog. (This seems to be a sequential U-turn: happening on OS 2.3.6 and 4.2.2 ... I haven't tested the other versions yet.)

0
source

Source: https://habr.com/ru/post/1369369/


All Articles