【发布时间】:2017-10-24 21:23:40
【问题描述】:
这个sn-p代码我尝试检索notes表的所有行,但在执行过程中返回的行数是正确的,但都像最后一行..我想知道错误是什么
public ArrayList<Note> selectAllNotes() {
Cursor cursor = dbReader.rawQuery("SELECT * FROM notes", null);
Note note = new Note();
ArrayList<Note> notes = new ArrayList<>();
while (cursor.moveToNext()) {
note.setNoteID(cursor.getInt(cursor.getColumnIndex(DbHelper.ID)));
note.setNoteTitle(cursor.getString(cursor.getColumnIndex(DbHelper.TITLE)));
note.setNoteContent(cursor.getString(cursor.getColumnIndex(DbHelper.CONTENT)));
notes.add(note);
}
return notes;
}
【问题讨论】:
-
移动你的笔记 note=new Note();到while循环里面。列表的所有内容都指向同一个对象。