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

}

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)
user2064070
  • 305
  • 3
  • 13

1 Answers1

0

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

}
pgiitu
  • 1,671
  • 1
  • 15
  • 23
  • Hi, thanks for help. Solution seems ok, but I get error in line – user2064070 Oct 09 '15 at 01:01
  • AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); Error: The method getActivity() is undefined for the type SoundAnalyzer – user2064070 Oct 09 '15 at 17:32
  • You should have a `context` somewhere so you can use that instead of `getActivity()` – pgiitu Oct 09 '15 at 18:11
  • 5
    I've replaced GetActivity with this and now it shows dialog. But still have problem that all choices appear as selectable (I'm testing proposed code). It seems that setEnabled(false) doesn't work in this case. Any suggestion ? – user2064070 Oct 10 '15 at 14:01