Android Room LiveData选择查询参数 [英] Android Room LiveData select query parameters

查看:616
本文介绍了Android Room LiveData选择查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定通过做一个简单的数据库应用程序来自学Java和Android.我已经以惰性"方式实现了某些功能-所有选择都在主线程上完成.

I decided to teach myself Java and Android by doing a simple database app. I already implemented some functionality the "lazy" way - all selects are done on the main thread.

现在,我想使用LiveData进行选择.我已经阅读了有关Android开发人员的简单培训指南,实施了 codelabs指南中更复杂的解决方案, LiveData和RecyclerView.整个表的插入,更新,删除和选择 都可以正常工作,但是我不知道如何将参数传递给选择.

Now I want to use LiveData for selects. I've read the simplistic training guide on android developers and implemented a more complex solution from codelabs guide, with LiveData and RecyclerView. Inserts, updates, deletes, and selects for whole tables work flawlessly, however I have no idea how to pass parameters to selects.

示例:我有一个活动,其中包含所有记录的可滚动列表,并且我想对列表(搜索)应用一些过滤器.据我了解,DAO的实际选择方法仅被调用一次(创建ViewModel时),因此如何使用新参数更新查询?

Example: I have an activity with scrollable list of all records and I want to apply some filters to the list (search). From what I understand the actual select method from DAO is called only once (when ViewModel is created), so how do I update query with new parameters?

其他示例:我还有其他活动,显示记录的所有列(用于查看和编辑). 如何传递ID来查询以选择单个行?

Other example: I have other activity that displays all columns of a record (for viewing and editing). How do I pass id to query to select a single row?

我的数据库代码与 编辑:我终于做到了:每次我想更新查询参数时,我都会调用DAO中的select(通过ViewModel和Repo)并将新的观察者添加到该新列表中.该解决方案似乎不是最佳解决方案,但我想它可以起作用...

I finally did it like that: every time I want to update query parameters I call select from DAO (through ViewModel and Repo) and add a new observer to that new list. This solution doesn't seem optimal but I guess it works...

推荐答案

我认为您的解决方案是Transformations.switchMap.最简单的示例,在代码实验室中,它可能如何工作

I think your solution is a Transformations.switchMap. The simplest example how it may works in the case from codelab

public class WordViewModel extends AndroidViewModel {
    private WordRepository mRepository;
    private LiveData<List<Word>> mAllWords;
    private LiveData<List<Word>> searchByLiveData;
    private MutableLiveData<String> filterLiveData = new MutableLiveData<>();

    public WordViewModel (Application application) {
        super(application);
        mRepository = new WordRepository(application);
        mAllWords = mRepository.getAllWords();
        searchByLiveData = Transformations.switchMap(filterLiveData, 
                                                     v -> mRepository.searchBy(v));
    }

    LiveData<List<Word>> getSearchBy() { return searchByLiveData; }
    void setFilter(String filter) { filterLiveData.setValue(filter); }
    LiveData<List<Word>> getAllWords() { return mAllWords; }
    public void insert(Word word) { mRepository.insert(word); }
}

因此,当您设置过滤器值时,它将更改searchByLiveData的值

So, when you set filter value, it will change value of searchByLiveData

希望对您有帮助.

这篇关于Android Room LiveData选择查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆