Android Room LiveData select query parameters
Asked Answered
C

1

14

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.

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.

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?

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?

My database code is more less the same as in this codelab

Edit: 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...

Carnes answered 24/2, 2018 at 19:56 Comment(6)
you can have multiple select statements with DaoDemeter
I thought of that, but let's say I want a text field in which user can type any search stringCarnes
so ? whats the issue here ?? though these doesnt require a select statement.Demeter
you have your list from select all statement...then from that whole list filter the list which the user searches...and show the searched listDemeter
hmmm... so basically filter using java not sql... maybe I'll try. Thanks for the ideaCarnes
yes..there are a lot of demos.. you can do whichever way you feel like.. :) it depends upon the functionDemeter
H
36

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); }
}

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

I hope this helps you.

Halvah answered 5/4, 2018 at 10:1 Comment(9)
Finally got some time to try your solution and it works wonderfully! Thank you so much! That's exactly what I was looking for.Carnes
In repository.searchBy(v) it must be mRepository?Anaplasty
@Anaplasty yes, i fix it. regarding the error, show me the code and perhaps i will be able to help youHalvah
in the line void setFilter(String filter) { filterLiveData.value = filter; } the ".value" does not exist in filterLiveDataAnaplasty
@Anaplasty setValue, sureHalvah
@Halvah it was helpful for me where I had to fetch data from Room based on various sort options selected by the user. Earlier, I was doing it in a very unusual way. Thanks!Woundwort
Thank you very much! This has resolved a fair bit of headache for me :)Jacqualinejacquard
Your code helped a lot. But there is a problem in the line : private LiveData<String> filterColor = new MutableLiveData<String>();instead use private MutableLiveData<String> filterColor = new MutableLiveData<String>(); because the LiveData's setValue() has a protected access while MutableLiveData's setValue() has a public access.Aphorize
have save mylifeVerla

© 2022 - 2024 — McMap. All rights reserved.