Esteban Esteban -3 years ago 131
Java Question

Multichoice AlertDialog get selected values as Strings

I get a

from server which I show as fields in
MultiChoice AlertDialog
. When the user checks a field I place it in another
. But I only get them as integers. How can I know which one was checked because I need the name of the item?

List<Integer> mSelectedItems = new ArrayList<>();

public void renderGenreList(List<Genre> genreList) {
CharSequence[] genreChar = convertGenreList(genreList);

private CharSequence[] convertGenreList(List<Genre> genreList) {
List<String> genreString = new ArrayList<>();
for (int i = 0; i < genreList.size(); i++) {
Genre genre = genreList.get(i);
return genreString.toArray(new CharSequence[genreString.size()]);

private void genreDialog(CharSequence[] genres) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
.setMultiChoiceItems(genres, null, (dialog, which, isChecked) -> {
if (isChecked) {
} else if (mSelectedItems.contains(which)) {
.setPositiveButton(R.string.OK_button, (dialog, which) -> {

.setNegativeButton(R.string.CANCEL_button, (dialog, which) -> dialog.dismiss());
d = builder.create();

Answer Source

Your mSelectedItems list has the list of selected items position. So you can do below to get all items

for (int i = 0; i < mSelectedItems.size(); i++) {
        Genre genre = genreList.get(mSelectedItems.get(i));
        // Here genre is one of the items selected and you can get all items in this loop

or second method is when you are adding values to mSelectedItems instead of adding which get the value and then add

replace -




then you will have mSelectedItems with list of selected items. Update the above for remove method as well.

the third way is to have instance of your dialog and get from there.

AlertDialog mAlert = builder.create();
// Display the alert dialog on interface;

// get list of selected items
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download