我已经开始研究最近引入的android架构组件。
DataLiveList.getPhotoDataList().observe(PhotosFragmentNew.this, new Observer<List<PhotoDataLive>>() {
@Override
public void onChanged(@Nullable List<PhotoDataLive> photoDataLives) {
}
});
我的问题是我只想要最后添加的元素而不是数据库中所有项目的列表,其次我想知道新模型的自动生成 id,在插入元素时获取该 id 的最佳方法是什么。
我的方法做错
了DataLiveList.getPhotoDataList()
返回一个列表,即我的 DAO 类有来自 PhotoModel 的 select * 的查询,我只需要在 DAO 类中编写另一个方法
@Query("Select * from photomodel order by Id desc limit 1")
LiveData<photomodel> getLatestAddedItem();
现在当我改变以下
DataLiveList.getPhotoDataList().observe(PhotosFragmentNew.this, new Observer<List<PhotoDataLive>>() {
@Override
public void onChanged(@Nullable List<PhotoDataLive> photoDataLives) {
}
});
自
DataLiveList.getLatestAddedItem().observe(PhotosFragmentNew.this, new Observer<PhotoDataLive>() {
@Override
public void onChanged(@Nullable PhotoDataLive photoDataLives) {
}
});
这将导致我想要的