【发布时间】:2018-02-05 05:36:14
【问题描述】:
我正在使用 Android LiveData 和房间数据库来更新列表。
我有一个笔记列表,其中有一个勾号将它们标记为“完成”。
如果它们被标记为完成,我将它们从当前列表(而不是从数据库)中删除,以便它们可以显示在另一个页面上。
但将它们标记为完成并不会更新我的列表。
道:
@Dao
public interface NoteDao {
@Query("SELECT * FROM notes WHERE isDone=0")
LiveData<List<Note>> getAll();
@Insert
void insert(Note note);
@Delete
void delete(Note note);
@Update
void update(Note note);
}
我的适配器中有一个 onchecked 侦听器,用于处理列表点击。
Note note = allNotes.get(position);
holder.mCheckBox.setListener(isChecked -> {
note.setIsDone(1);
mNoteDao.update(note);
notifyDataSetChanged();
});
我在活动中加入了一个观察功能。
mNoteDao.getAll().observe(this, notes -> {
if (notes != null && notes.size() > 0) {
img_no_notes.setVisibility(View.GONE);
txt_no_notes.setVisibility(View.GONE);
Collections.reverse(notes);
mAdapter.setItems(notes);
} else {
img_no_notes.setVisibility(View.VISIBLE);
txt_no_notes.setVisibility(View.VISIBLE);
}
});
列表在重新启动时更新。 感谢您的帮助。
【问题讨论】:
-
能否请您展示 mAdapter 的 setItems() 方法?
标签: android android-room android-livedata