I have this code to show a dialog with singlechoice(radio) options.
AlertDialog ad = new AlertDialog.Builder(this)
.setCancelable(false)
.setIcon(R.drawable.
I tried to use ListView.setSelection(int)
but it never worked as expected so instead I decided to make use of View.setTag() to temporarily store the selected position.
.setSingleChoiceItems(adapter, -1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ListView lv = ((AlertDialog)dialog).getListView();
lv.setTag(new Integer(which));
}
})
The tag can then be accessed easily after a button click.
.setPositiveButton(R.string.button_text,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ListView lv = ((AlertDialog)dialog).getListView();
Integer selected = (Integer)lv.getTag();
if(selected != null) {
// do something interesting
}
}
})