如何修复“数组类型预期发现java.util.arraylist”?

时间:2015-11-11 19:20:08

标签: java android

鉴于以下功能,AndroidStudio在标记行中给出错误:

array type expected found java.util.arraylist

我还尝试使用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();
}

1 个答案:

答案 0 :(得分:21)

更改

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

到:

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

并更改

protected void multiSelect(final ArrayList items)

protected void multiSelect(final ArrayList<String> items)

<强>更新

setItems的{​​{1}}方法需要DialogBuilder,而不是array