Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ListItemViewModel extends ViewModel {
- private MediatorLiveData<ItemList> mList;
- private MeliRepository meliRepository;
- /* Empty Contructor.
- * To have a ViewModel class with non-empty constructor,
- * I have to create a Factory class which would create instance of you ViewModel and
- * that Factory class has to implement ViewModelProvider.Factory interface.
- */
- public ListItemViewModel(){
- meliRepository = new MeliRepository();
- }
- public LiveData<ItemList> getItemList(String query){
- if(mList == null){
- mList = new MediatorLiveData<>();
- LoadItems(query);
- }
- }
- private void LoadItems(String query){
- String queryToSearch = TextUtils.isEmpty(query) ? "IPOD" : query;
- mList.addSource(
- meliRepository.getItemsByQuery(queryToSearch),
- list -> mList.setValue(list)
- );
- }
- }
Add Comment
Please, Sign In to add comment