Skip to content Skip to sidebar Skip to footer

Android: Alertdialog - How To Disable Certain Choices That Are Not Available

I have AlertDialog that lets user to pick up one of available choices. I have 7 choices and have separate array where 1 and 0 describe whether choice is valid or not. Then I do thi

Solution 1:

You need to use the Adapter to access the views. Also it should be done after the alert.show() because until then the Adapter is null.

Here is the modified code I wrote with some test data. There is no crash with this:

publicvoidcreateListAlertDialog() {
    ListView list;
    AlertDialog.Builderbuilder=newAlertDialog.Builder(getActivity());
    builder.setTitle("Pick a Sampling Rate");
    String[] SampleRates_Items = {
            "test1", "test2", "test3"
    };
    builder.setSingleChoiceItems(SampleRates_Items, SampleRates_Index,
            newDialogInterface.OnClickListener() {
                publicvoidonClick(DialogInterface dialog, int item) {
                    SampleRates_Index = item;

                }
            });

    builder.setPositiveButton("OK", newDialogInterface.OnClickListener() { // Add the OK buttonpublicvoidonClick(DialogInterface dialog, int which) {
                    //PickFsDone = true;
                }
            }

    );

    AlertDialogalert= builder.create();

    alert.show();
    list = alert.getListView();
    finalListAdapteradaptor= alert.getListView().getAdapter();
    for (inti=0; i < SampleRates_Items.length; i++) { // indexif (i % 2 == 0) {
            // Disable choice in dialog
            adaptor.getView(i, null, list).setEnabled(false);
        } else {
            // Enable choice in dialog
            adaptor.getView(i, null, list).setEnabled(true);
        }
    }

}

Post a Comment for "Android: Alertdialog - How To Disable Certain Choices That Are Not Available"