Android : how to make the alertDialog options mutually-exclusive

on Sunday, September 7, 2014


I want to make a simple dialog alert with 3 options (800m, 1300m, 2000m), that are mutually exclusive. Below you can see how i created the AlertDialog using the Builder. I setted the default selected item to 0, and i would like to be able to change the selected item according to the "which" parameter of the clickListener. getSelectedItem position always returns -1 and setItemChecked doesn't seem to work either.



public static class ZoomTypes extends DialogFragment {
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.menu_radius)
.setSingleChoiceItems(R.array.menu_radius_array, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

((AlertDialog) dialog).getListView().getSelectedItemPosition(); // this is -1
((AlertDialog) dialog).getListView().setItemChecked(which, true); //doesn't work
((AlertDialog) dialog).hide();

}
});
return builder.create();
}
}


and this are my menu items



<string-array
name="menu_radius_array">
<item
>800m
</item>
<item
>1300m
</item>
<item
>2000m
</item>
</string-array>


Any ideea on how to update the checkedItem inside the onClickListener event ?


0 comments:

Post a Comment