Java 查询sqlite数据库有outofboundException

Java 查询sqlite数据库有outofboundException,java,android,sql,sqlite,android-sqlite,Java,Android,Sql,Sqlite,Android Sqlite,我有一个ArrayList,我试图将其转换为字符串[],这样我就可以查询我的数据库,但是我得到了outofboundException,我不知道问题出在哪里 要将arraylist转换为字符串[],请执行以下操作: String[] idsArray = new String[selectedTopicIds.size()]; idsArray = selectedTopicIds.toArray(idsArray); for (int i = 0; i < selecte

我有一个
ArrayList
,我试图将其转换为字符串[],这样我就可以查询我的数据库,但是我得到了
outofboundException
,我不知道问题出在哪里

要将arraylist转换为字符串[],请执行以下操作:

String[] idsArray = new String[selectedTopicIds.size()];
    idsArray = selectedTopicIds.toArray(idsArray);
    for (int i = 0; i < selectedTopicIds.size(); i++) {
        idsArray[i] = selectedTopicIds.get(i);
    }
这就是光标循环的方式:

if (cursor.moveToFirst()) {
        do {
            Word word = new Word();
            word.setId(cursor.getInt(0));
          
            wordList.add(word);

        } while (cursor.moveToNext());
    }
    cursor.close();
在我给它的那行
idsArray
我有一个例外:

java.lang.IllegalArgumentException:无法绑定索引35处的参数 因为索引超出范围。该语句有1个参数

arraylist和String[]都必须具有索引0到34之间的35个元素


我不明白问题出在哪里?

您是否试图将列
Util.KEY\u主题
与数组
idsArray
的项相匹配?
如果是这样,则不应使用运算符
=
,而应使用
中的运算符
,并构造方法
query()
选择
参数,以便它包含的
占位符与
idsrarray
的项数一样多

一种更简单的方法是创建一个逗号分隔的列表,其中包含
idsArray
的所有项,类似于
“,1,2,3”
数组。toString()

并使用运算符
,如

Cursor cursor = myDb.query(
    Util.TABLE_NAME,
    null,
    "? LIKE '%,' || " + Util.KEY_TOPIC + " || ',%'",
    new String[] {ids},
    null,
    null,
    null
);

这意味着数组中的索引35不存在。因此,您的数组中没有足够的项,您正在调用不存在的东西。您的问题并发布整个堆栈跟踪(请参见logcat)
该语句有1个参数。
意味着您的查询语句只包含一个
字符。我知道,但如何使光标不调用索引35?我添加了光标循环函数index 35意味着它要求第36个元素
String ids = Arrays.toString(idsArray).replaceAll("[\\[\\]]", ",").replace(" ", "");
Cursor cursor = myDb.query(
    Util.TABLE_NAME,
    null,
    "? LIKE '%,' || " + Util.KEY_TOPIC + " || ',%'",
    new String[] {ids},
    null,
    null,
    null
);