Выбранный элемент - это элемент с индексом item
, соответствующая строка - items[item]
.
Типичный шаблон, чтобы поймать выбор, выглядит так:
public class SomeDialog {
final CharSequence[] items = { "3 sec", "5 sec", "7 sec" };
int choice = -1;
//...
public void someMethod() {
//...
builder.setSingleChoiceItems(items, -1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item],
Toast.LENGTH_SHORT).show();
choice = item; // <-- now we store the selection to a class member
}
});
// ...
}
public String getSelection() {
return items[choice]; // <-- this allows another class to read the current selection
// often called after the dialog has been closed
}
}
EDIT
Альтернативой было бы сделать диалог (или часть диалога) наблюдаемым . Диалог будет запускать событие всякий раз, когда выбор изменяется, и любой, заинтересованный в выбранном значении, будет слушать этот источник события.