Android: AlertDialog - how to disable certain choices that are not available

拈花ヽ惹草 提交于 2019-12-25 04:07:50

问题


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 this :

public void createListAlertDialog() {
ListView list;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a Sampling Rate");
builder.setSingleChoiceItems(SampleRates_Items, SampleRates_Index, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
        SampleRates_Index = item;
    }
});

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {   //Add the OK button
        public void onClick(DialogInterface dialog, int which) {
            PickFsDone = true;
       }
}


);

AlertDialog alert = builder.create();

list = alert.getListView();  // *** I get crash on this line...
for (int i = 0; i < (SampleRates_Num); i++) {    // index
    if (SampleRates_Valid[i] == 0) {
        // Disable choice in dialog 
        list.getChildAt(i).setEnabled(false);
    } else {
        // Enable choice in dialog 
        list.getChildAt(i).setEnabled(true);
}
}
alert.show();

}

I get crash in line marked with // *** ... What am I doing wrong? I must be missing something obvious... What I want to do is to disable choices that are marked with 0 in SampleRates_Valid[x].

UPDATE: Crash happens on other two lines with SetEnabled method. Here is crash log :

10-09 02:33:18.624: D/AndroidRuntime(7105): Shutting down VM
10-09 02:33:18.624: E/AndroidRuntime(7105): FATAL EXCEPTION: main
10-09 02:33:18.624: E/AndroidRuntime(7105): Process: processing.test.soundanalyzer, PID: 7105
10-09 02:33:18.624: E/AndroidRuntime(7105): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setEnabled(boolean)' on a null object reference
10-09 02:33:18.624: E/AndroidRuntime(7105):     at processing.test.soundanalyzer.SoundAnalyzer.createListAlertDialog(SoundAnalyzer.java:995)
10-09 02:33:18.624: E/AndroidRuntime(7105):     at processing.test.soundanalyzer.SoundAnalyzer$5.run(SoundAnalyzer.java:1014)
10-09 02:33:18.624: E/AndroidRuntime(7105):     at android.os.Handler.handleCallback(Handler.java:815)
10-09 02:33:18.624: E/AndroidRuntime(7105):     at android.os.Handler.dispatchMessage(Handler.java:104)
10-09 02:33:18.624: E/AndroidRuntime(7105):     at android.os.Looper.loop(Looper.java:194)
10-09 02:33:18.624: E/AndroidRuntime(7105):     at android.app.ActivityThread.main(ActivityThread.java:5534)
10-09 02:33:18.624: E/AndroidRuntime(7105):     at java.lang.reflect.Method.invoke(Native Method)
10-09 02:33:18.624: E/AndroidRuntime(7105):     at   java.lang.reflect.Method.invoke(Method.java:372)
10-09 02:33:18.624: E/AndroidRuntime(7105):     at    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
10-09 02:33:18.624: E/AndroidRuntime(7105):     at      com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)

回答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:

public void createListAlertDialog() {
    ListView list;
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Pick a Sampling Rate");
    String[] SampleRates_Items = {
            "test1", "test2", "test3"
    };
    builder.setSingleChoiceItems(SampleRates_Items, SampleRates_Index,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    SampleRates_Index = item;

                }
            });

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { // Add the OK button
                public void onClick(DialogInterface dialog, int which) {
                    //PickFsDone = true;
                }
            }

    );

    AlertDialog alert = builder.create();

    alert.show();
    list = alert.getListView();
    final ListAdapter adaptor = alert.getListView().getAdapter();
    for (int i = 0; i < SampleRates_Items.length; i++) { // index
        if (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);
        }
    }

}


来源:https://stackoverflow.com/questions/33027918/android-alertdialog-how-to-disable-certain-choices-that-are-not-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!