Reputation: 2707
I am using AlertDialog with Multiple choice to show list of check-able items.
When user selects some of values, I can get theirs index and save it to list. That is working fine.
But I want when user again open AlertDialog to have selected/checked values that he selected before.
Here is the code:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMultiChoiceItems(R.array.array_cousine, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int indexSelected,
boolean isChecked) {
if (isChecked) {
seletedItems.add(++indexSelected);
} else if (seletedItems.contains(indexSelected)) {
seletedItems.remove(Integer.valueOf(++indexSelected));
}
}
})
// Set the action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String[] expList = getResources().getStringArray(R.array.array_cousine);
for (int i = 0; i < seletedItems.size(); i++) {
int selected = seletedItems.get(i);
String selectedString = expList[selected - 1];
selectedItemsName.add(selectedString);
}
StringBuilder stringBuilder = new StringBuilder();
for (int j = 0; j < selectedItemsName.size(); j++) {
String text = selectedItemsName.get(j);
stringBuilder = stringBuilder.append(" "+text);
}
Log.d("TAG", "String builder: " + stringBuilder);
tvCusine.setText(stringBuilder);
dialog.dismiss();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
Dialog dialog = builder.create();//AlertDialog dialog;
dialog.show();
Here is the picture:
Upvotes: 1
Views: 3249
Reputation: 657
The second parameter in builder.setMultiChoiceItems
is a boolean[]
that you are currently passing in as null. To show items as checked when it opens pass in this array with true
in the position of each item that you want to be checked. These values can be set after the array is created using boolean[position] = value
Upvotes: 6
Reputation: 1650
If you take a look at docs for setMultiChoiceItems second argument is boolean array in which you set which items are checked and which are not. You are passing null, therefore, nothing will be checked.
Upvotes: 1