HotLovingCoder3
HotLovingCoder3

Reputation: 23

Trouble setting items in AlertDialog with OnClickListener

Having trouble trying to set up a spinner withing an AlertDialog, keep getting the error "The method setItems(int, DialogInterface.OnClickListener) in the type AlertDialog.Builder is not applicable for the arguments (String[], new View.OnClickListener(){})"

I'm fairly new to Android programming and I am still getting used to it, any help would be greatly appreciated! Thanks

AlertDialog.Builder b = new Builder(this);
    b.setTitle("Select Day");
    String[] types = {"1", "2", "3"};
    b.setItems(types, new OnClickListener){
        public void onClick(DialogInterface dialog, int which){
            dialog.dismiss();
            switch(which){
            case 0:
                day = "1";
                break;
            case 1:
                day = "2";
                break;
            }
        }


    });

Upvotes: 0

Views: 1403

Answers (1)

Raghav Sood
Raghav Sood

Reputation: 82563

Change:

b.setItems(types, new OnClickListener){

To

b.setItems(types, new DialogInterface.OnClickListener){

And you will have to split

String[] types = {"1", "2", "3"};

to individual ints, or a CharSequence array.

You probably have an OnClickListener for another normal View in your code somewhere, and have it in your imports. As all OnClickListener classes share the same name, Eclipse auto resolves them all to the currently imported one. You can specify the parent class in such cases.

Additionally, the setItems() method that Eclipse has resolved takes a single int for the first parameter, not a String array. You could use the other setItems() method though, which takes a CharSequence Array. In such a case, change

String[] types = {"1", "2", "3"};

to

CharSequence[] types = {"1", "2", "3"};

Upvotes: 3

Related Questions