如何修复“预期找到的数组类型 java.util.arraylist”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33658225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:53:11  来源:igfitidea点击:

How to fix "array type expected found java.util.arraylist"?

javaandroid

提问by Alex

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

给定如下函数,AndroidStudio 在标记行中给出错误:

array type expected found java.util.arraylist

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

我也尝试使用get而不是直接引用,但是 Android Studio 告诉我一些setItems无法解决的问题。代码在这里:

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

采纳答案by Mohammed Aouf Zouag

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 setItemsmethod of the DialogBuilderexpects an array, not an arrayList.

setItems方法DialogBuilder需要一个array,而不是一个arrayList