在首选项更改侦听器的基础上更改android视图模型的查询参数

时间:2018-08-11 08:38:10

标签: android android-room android-architecture-components

我在我的应用程序中使用体系结构组件。我使用了分页,但是我有一个问题,如果根据首选项更改了参数,如何重新查询数据库。由于视图模型是在oncreate方法中设置的,因此我们在视图模型构造函数内部的Dao接口上调用查询。

我试图在onPreferenceChangeLitener内重新创建视图模型,但是它不起作用。

我的主要活动是-

public class MainActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener{

    private static final String TAG = MainActivity.class.getSimpleName();

    RecyclerView rvDisplayMovie;
    MovieAdapter mAdapter;

    MainActivityViewModel mViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rvDisplayMovie = (RecyclerView) findViewById(R.id.rv_display_movies);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvDisplayMovie.setLayoutManager(linearLayoutManager);
        mAdapter = new MovieAdapter();

        rvDisplayMovie.setAdapter(mAdapter);

        MainViewModelFactory factory = InjectorUtils.provideMainViewModelFactory(this);

        mViewModel = ViewModelProviders.of(this, factory).get(MainActivityViewModel.class);

        mViewModel.getMovies().observe(this, new Observer<PagedList<MovieEntry>>() {
            @Override
            public void onChanged(@Nullable PagedList<MovieEntry> movieEntries) {
                Log.d(TAG,"Movie list changed"+movieEntries.size());
                mAdapter.submitList(movieEntries);
            }
        });

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        sharedPreferences.registerOnSharedPreferenceChangeListener(this);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_settings,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                Intent intent = new Intent(this, SettingsActivity.class);
                startActivity(intent);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
        mViewModel.triggerPrefsChenged(this);
    }
}

和视图模型是-

public class MainActivityViewModel extends ViewModel {

    private static final String TAG = MainActivityViewModel.class.getSimpleName();

    private PopularMovieRepository mRepository;
    private LiveData<PagedList<MovieEntry>> mMovies;

    public MainActivityViewModel(PopularMovieRepository repository, int movieType){
        mRepository = repository;
        mMovies = mRepository.getMoviesList(movieType);
    }

    public  LiveData<PagedList<MovieEntry>> getMovies() {
        return mMovies;
    }

    public void triggerPrefsChenged(Context context){
        int movieType = PreferenceUtils.getMovieType(context);
        mRepository.initializeData(movieType);
    }
}

triggerPrefsChenged方法下载新数据并将其插入数据库中

1 个答案:

答案 0 :(得分:1)

您的方法有一个问题,就是您的mMovies被硬编码为初始movieType(您在MainActivityViewModel中传递的那个)。您应该修改mMovies的用法,以便它可以对movieType的更改做出反应。

执行此操作的一个好方法是创建一个附加的LiveData对象,该对象侦听movieType的更改,然后使用mRepository.getMoviesList(movieType)触发具有新类型的Transformations.switchMap调用。因此,您的ViewModel可能如下:

public class MainActivityViewModel extends ViewModel {

    private static final String TAG = MainActivityViewModel.class.getSimpleName();
    private PopularMovieRepository mRepository;
    private final MutableLiveData<Integer> mMovieTypeLiveData = new MutableLiveData();
    private final LiveData<PagedList<MovieEntry>> mMovies;

    public MainActivityViewModel(PopularMovieRepository repository, int movieType){
        mRepository = repository;
        mMovies = Transformations.switchMap(movieTypeLive, (movieType) -> {
            return mRepository.getMoviesList(movieType);
        });
        //set the initial value
        mMovieTypeLiveData.setValue(movieType);
    }

    public LiveData<PagedList<MovieEntry>> getMovies() {
        return mMovies;
    }

    public void triggerPrefsChenged(Context context){
        int movieType = PreferenceUtils.getMovieType(context);
        //mRepository.initializeData(movieType);
        mMovieTypeLiveData.setValue(movieType);
    }
}