我是 android 新手,我的光标有问题。我无法使用以下方式访问数据:
cursor.get(cursor.getColumnIndex(columnName));
我尝试了以下代码来测试错误:
while (cursor.moveToNext()) {
int x = 2;
Log.i("MyDebug", "Index: " + x);
Log.i("MyDebug", "Name: " + cursor.getColumnName(x));
Log.i("MyDebug", "Index again: " + cursor.getColumnIndex(cursor.getColumnName(x)));
}
调试监视器的结果:
Index: 2
Name: mainMenu.title
Index again: -1
“再次索引”的结果不应该是2吗?我做错了什么?
最佳答案
cursor.getColumnIndex()
需要列的名称,没有表的名称:
cursor.getColumnIndex("mainMenu.title"); // -1
cursor.getColumnIndex("title"); // 2
关于Android getColumName 和 getColumnIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4511894/