Android:AlertDialog-如何禁用某些不可用的选择 [英] Android: AlertDialog - how to disable certain choices that are not available

查看:166
本文介绍了Android:AlertDialog-如何禁用某些不可用的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

}

我在标有//***的行中崩溃—我在做什么错?我必须缺少一些明显的东西.我要做的是禁用在SampleRates_Valid[x]中标记为0的选择.

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].

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

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)

推荐答案

您需要使用适配器访问视图.还应该在alert.show()之后执行此操作,因为在此之前Adapternull.

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

}

这篇关于Android:AlertDialog-如何禁用某些不可用的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆