I implemented the google example about alertDialog with multichoititem, in this class:
@NotNull
public Dialog onCreateDialog(Bundle savedInstanceState){
final boolean[] qtdClassif = new boolean[25];
ArrayList classifica = new ArrayList<>();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Escolha até 3 categorias").setMultiChoiceItems(R.array.classifBovino, qtdClassif, new DialogInterface.OnMultiChoiceClickListener() {
int contador = 0;
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
contador += isChecked ? 1 : -1;
qtdClassif[which] = isChecked;
if(contador > 3){
Toast.makeText(getContext(), "+3 category added, unselect someone", Toast.LENGTH_SHORT).show();
qtdClassif[which] = false;
contador --;
((AlertDialog) dialog).getListView().setItemChecked(which, false);
}else{
classifica.add(which);
}
}
});
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getContext(), "category: "+String.valueOf(classifica.get(0)), Toast.LENGTH_SHORT).show(); // He return to me the index from R.array.classifBovino, not the text from R.array.classifBovino that was selected.
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getContext(), "No category ", Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
}
But, this arraylist classifica just have the "positions" of the selected item, which is not very useful, because we usually want the value that the user selected in text (the value of the field, not its position in the list), what I tried was: ((AlertDialog) dialog).getListView()...
but the methods listed here do not clarify my idea of obtaining the text of the selected fields, and not their position