游标可以被认为是指向一些底层数据的指针。在游标对象上运行c.toString() 将打印其字符串表示的默认Cursor 类的实现(@ 的符号字符和对象哈希码的无符号十六进制表示),这不是您想要的.
要检索基础数据库数据,您需要调用c.getString(columnIndex) (source),或该特定列索引所需的任何列数据类型。
这是一个例子modified from source:
假设你已经创建了一个表
private static final String DATABASE_CREATE =
"create table comments ( "
+ "_id integer primary key autoincrement, "
+ "comment text not null);";
您的getAllGoals 函数返回一个光标,该光标指向关于两者 _id 和comment 的数据。现在您只想显示有关comment 列的详细信息。所以你必须运行c.getString(1)。假设您的getAllGoals 函数返回一个游标,该游标仅指向有关comment 列的数据。现在你必须运行c.getString(0)。
我建议您下载提供的示例中的源代码,并了解如何从游标中检索数据。
编辑:
public List<Comment> getAllComments() {
List<Comment> comments = new ArrayList<Comment>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {//retrieve data from multiple rows
Comment comment = cursorToComment(cursor);
comments.add(comment);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return comments;
}
private Comment cursorToComment(Cursor cursor) {
Comment comment = new Comment();
comment.setId(cursor.getLong(0));
comment.setComment(cursor.getString(1));
return comment;
}
source