这可能是一个简单答案的愚蠢问题但是当从记事本示例中使用简单的游标适配器时,我从我的数据库中获取了一个名单列表。
当我尝试通过将光标移动到行并提取名称时“手动”执行此操作时,光标显示返回零行...
这符合以下示例:
Cursor notesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(notesCursor);
// Create an array to specify the fields we want to display in the list (only NAME)
String[] from = new String[]{WeightsDatabase.KEY_NAME};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.weightrows};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.weights_row, notesCursor, from, to);
setListAdapter(notes);
现在我正在努力做到这一点,但它并没有完全奏效。简单的游标适配器是不是做了特别的事情我不是吗?
//check # of rows, returns 0
int mOne = notesCursor.getCount();
//initial random string array
String[] temp = new String[100];
int i = 0;
notesCursor.moveToFirst();
do{
temp[i]=notesCursor.getString(notesCursor.getColumnIndex(WeightsDatabase.KEY_ROWID)); //crashes here
//index out of bounds
i++;
}while(notesCursor.moveToNext());
我已经使用了这个,但是返回一个特定的查询,例如返回名为“ _ ”的所有行。返回所有笔记有什么不同?
答案 0 :(得分:0)
moveToFirst()
实际上返回boolean
,因此您可以在尝试从Cursor
读取之前检查该值来防止抛出异常:
if (notesCursor.moveToFirst()) {
// do loop
}
至于为什么有0行,你是否试图重复使用传递给SimpleCursorAdapter
的相同游标,或者是独立失败的代码?如果您尝试重新使用它,我会在执行新的Cursor
后使用新的fetchAllNotes()
进行尝试。