Guest User

Untitled

a guest
Jan 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. public class ListItemViewModel extends ViewModel {
  2. private MediatorLiveData<ItemList> mList;
  3. private MeliRepository meliRepository;
  4.  
  5. /* Empty Contructor.
  6. * To have a ViewModel class with non-empty constructor,
  7. * I have to create a Factory class which would create instance of you ViewModel and
  8. * that Factory class has to implement ViewModelProvider.Factory interface.
  9. */
  10. public ListItemViewModel(){
  11. meliRepository = new MeliRepository();
  12. }
  13.  
  14. public LiveData<ItemList> getItemList(String query){
  15. if(mList == null){
  16. mList = new MediatorLiveData<>();
  17. LoadItems(query);
  18. }
  19. }
  20.  
  21. private void LoadItems(String query){
  22. String queryToSearch = TextUtils.isEmpty(query) ? "IPOD" : query;
  23.  
  24. mList.addSource(
  25. meliRepository.getItemsByQuery(queryToSearch),
  26. list -> mList.setValue(list)
  27. );
  28. }
  29. }
Add Comment
Please, Sign In to add comment