Android:AlertDialog - 如何禁用某些不可用的选项

时间:2015-10-09 00:10:54

标签: android android-alertdialog

我有AlertDialog,可以让用户选择一个可用的选项。 我有7个选择并且有单独的数组,其中1和0描述选择是否有效。然后我这样做:

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();

}

我在标有// ***的行中崩溃......我做错了什么?我必须遗漏一些明显的东西......我想要做的是禁用SampleRates_Valid[x]中标记为0的选项。

更新:使用SetEnabled方法在其他两行上发生崩溃。 这是崩溃日志:

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 个答案:

答案 0 :(得分:0)

您需要使用适配器来访问视图。此外,它应该在alert.show()之后完成,因为在此之前Adapternull

这是我用一些测试数据编写的修改后的代码。这没有崩溃:

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);
        }
    }

}