jeudi 30 juin 2016

Android : How to disable one item in AlertDialog with MultiChoiceItems

I create a dialog with following code :

final CharSequence[] items        = {" One ", " Two ", " Three "};

AlertDialog dialog = new AlertDialog.Builder(this)
            .setTitle("Title1")
            .setMultiChoiceItems(items, null, null)
            .setPositiveButton("CLOSE", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    Log.e("1k", "count : " + ((AlertDialog) dialog).getListView().getChildCount());

                }
            }).show();
    ListView lw = dialog.getListView();
    //lw.getChildAt(0).setEnabled(false);
    Log.e("1k", "count : " + lw.getChildCount());

This creates a dialog. When I click on the "CLOSE" button I can see an output of "3" in the logs. So far so good, "items" array has 3 Strings in it.

The last line of code, which gets invoked after "show()", gives me "0" in the logs.

What I want to do is disabling the first item in the list, but this code throws a NullPointerException because "getChildAt(0)" returns null :

dialog.getListView().getChildAt(0).setEnabled(false);

How can I disable the first Item in the dialog's list ?

(and why does getChildCount() ..

.. return 0 instead of 3 when invoked after show() ?

.. return 3 as expected in onclick of PositiveButton ? )




Aucun commentaire:

Enregistrer un commentaire