我总是android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1在下面的代码中得到例外.请指出什么是错的.
public boolean Foo(String str) {
Cursor c = dbHelper.getItemByTitle(str);
if (c.getCount() == 0) {
return false;
} else {
c.getString(1) // Irrespective of what argument put here I get this exception
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
我没有使用Sqlite,但我怀疑你需要moveToNext()在询问值之前调用第一行:
if (!c.moveToNext()) {
return false;
}
String value = c.getString(1);
...
Run Code Online (Sandbox Code Playgroud)
请注意,getCount()返回结果集中的行数,但参数getString()指示列号.另请注意,就我从文档中看到的而言,这里的列号从零开始,与基于一的JDBC不同.