Android implement search with view model and live data

后端 未结 5 1633
灰色年华
灰色年华 2020-12-15 01:10

I\'m working on a project in android for a udacity course I\'m currently trying to implement a search function while adhering to android architecture components and using fi

5条回答
  •  执念已碎
    2020-12-15 01:52

    you can use Transformation.switchMap to do search operations.

    1. In viewmodel create MutableLiveData which has latest search string.

    2. Inside viewmodel use:

        LiveData data = 
        LiveDataTransformations.switchMap(searchStringLiveData, string ->  
        repo.loadData(string)))
    
    1. Return the above live data to activity so it can observe and update view.

提交回复
热议问题