我在这个类中用 multichoitem 实现了关于 alertDialog的谷歌示例:
@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();
}
}
但是,这个arraylist classifica只有被选中项的“位置”,这不是很有用,因为我们通常想要用户在文本中选择的值(字段的值,而不是它在列表中的位置),什么我试过的是:((AlertDialog) dialog).getListView()...
但是这里列出的方法并没有阐明我获取所选字段文本的想法,而不是它们的位置