Using the following lines of code, I can to display data from a backend server in a RecyclerView
using PagedListAdapter
.
movieViewModel = ViewModelProviders.of(this).get(MovieViewModel.class);
MovieAdapter adapter = new MovieAdapter(this);
movieViewModel.moviePagedList.observe(this, adapter::submitList);
recyclerView.setAdapter(adapter);
I created this method:
private void movieSearch(String searchText) {
globalSearch = searchText;
movieViewModel.replaceSubscription(this);
movieViewModel = ViewModelProviders.of(this).get(MovieViewModel.class);
MovieAdapter adapter = new MovieAdapter(this);
movieViewModel.moviePagedList.observe(this, adapter::submitList);
recyclerView.setAdapter(adapter);
}
Which is called from inside onQueryTextChange()
to display the results of the search but for some reasons the data in my RecyclerView
is not refreshed.
This is also my MovieViewModel
class:
public class MovieViewModel extends ViewModel {
LiveData<PagedList<ApiResponse.Movie>> moviePagedList;
public MovieViewModel() {
MovieDataSourceFactory movieDataSourceFactory = new MovieDataSourceFactory(search);
PagedList.Config config = new PagedList.Config.Builder().setEnablePlaceholders(false).setPageSize(20).build();
moviePagedList = new LivePagedListBuilder<>(movieDataSourceFactory, config).build();
}
void replaceSubscription(LifecycleOwner lifecycleOwner) {
moviePagedList.removeObservers(lifecycleOwner);
}
}
I everytime I search something, I need to get freash data. How to solve this?
PagedListAdapter
is a subclass ofandroid.support.v7.widget.RecyclerView.Adapter
so a call tonotifyDataSetChanged()
will do.