遊標可以被認爲是指向一些底層數據的指針。在光標對象上運行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
怎樣的信息?你能更具體化嗎? 'String g = c.toString()''你只能得到''cursor''的字符串表示。爲了從光標獲取信息,你應該使用'getters'和方法來允許你通過數據傳遞。 – Sajmon