0

我已经开始研究最近引入的 android 架构组件。

DataLiveList.getPhotoDataList().observe(PhotosFragmentNew.this, new Observer<List<PhotoDataLive>>() {
        @Override
        public void onChanged(@Nullable List<PhotoDataLive> photoDataLives)    {

        } 

    });

我的问题是我只想要最后添加的元素而不是数据库中所有项目的列表,其次我想知道为新模型自动生成的 id,在插入元素时获取该 id 的最佳方法是什么。

4

1 回答 1

0

我做错了我的方法

DataLiveList.getPhotoDataList()

返回一个列表,即我的 DAO 类有 select * from PhotoModel 的查询,我只需要在 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)    {

    } 

});

这将导致我想要的

于 2017-07-11T02:23:12.667 回答