7

Given the function as below, AndroidStudio gives an error in the labeled line:

array type expected found java.util.arraylist

I also tried to use get instead of a direct referencing, but then Android Studio is telling me something that setItems cannot be resolved. The code is here:

protected void multiSelect(final ArrayList items) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Selection")
            .setItems(items, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    Log.i("Select", "Selected entry: " + items[item]); // error here
                }
            });

    builder.create();
}

1 Answer 1

28

Change

Log.i("Select", "Selected entry: " + items[item]);

to :

Log.i("Select", "Selected entry: " + items.get(item));

and change

protected void multiSelect(final ArrayList items)

to

protected void multiSelect(final ArrayList<String> items)

UPDATE:

the setItems method of the DialogBuilder expects an array, not an arrayList.

Sign up to request clarification or add additional context in comments.

6 Comments

I have trying to tell that I tried this without success.
One sec... ArrayList of what ?
The error message is: Cannot resolve method 'setItems(java.util.ArrayList, anonymous android.content.DialogInterface..OnClickListener')
You should specify the type of the objects that your arrayList will hold reference to, like this : "final ArrayList<int> items" , for the example
With both suggestions I get the error: "Cannot resolve symbol 'items'" ... on all three occurrances of the variable 'items'.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.