ViewModel-在运行时观察LiveData时更改方法的参数? [英] ViewModel - change parameter of method while observing LiveData at runtime?

查看:184
本文介绍了ViewModel-在运行时观察LiveData时更改方法的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚MVVM(对我来说这是很新的东西),并且弄清楚了如何使用Room和ViewModel观察LiveData.现在我面临一个问题.

I'm trying to figure out MVVM (It's very new for me) and I figured out how to observe LiveData using Room and ViewModel. Now I am facing a problem.

我有一个需要参数的Room查询,这就是我开始在MainActivity的onCreate中观察LiveData的方式.

I have a Room query which requires parameter and this is how I start observing the LiveData in onCreate in MainActivity.

    String color = "red";
    myViewModel.getAllCars(color).observe(this, new Observer<List<Car>>() {
            @Override
            public void onChanged(@Nullable List<Car> cars) {
                adapter.setCars(cars);
            }
        });

通过使用此代码,我收到了红色"汽车的列表,并用该列表填充RecyclerView.

By using this code, I recieve list of "red" cars and populate RecyclerView with the List.

现在是我的问题-有没有办法改变 getAllCars 方法内的 color 变量(例如通过单击按钮)并影响观察者返回新的List?如果我只是更改color变量,则什么也不会发生.

Now to my question - is there a way to change the color variable inside the getAllCars method (for example by button click) and affect the observer to return new List? If i simply change the color variable, nothing happens.

推荐答案

在此答案中所述,您的解决方案是 Transformation.switchMap

来自 Android for Developers网站:

LiveData<&switchMap(LiveData< X>触发器,Function< X,LiveData< Y>> func)

LiveData< Y > switchMap (LiveData< X > trigger, Function< X, LiveData< Y >> func)

创建一个LiveData,将其命名为swLiveData,它遵循下一个流程:更改触发器LiveData,将给定函数应用于新值触发LiveData的设置,并将得到的LiveData设置为"backing".LiveData到swLiveData.支持"LiveData意味着所有事件它发出的信号将由swLiveData重新传输.

Creates a LiveData, let's name it swLiveData, which follows next flow: it reacts on changes of trigger LiveData, applies the given function to new value of trigger LiveData and sets resulting LiveData as a "backing" LiveData to swLiveData. "Backing" LiveData means, that all events emitted by it will retransmitted by swLiveData.

根据您的情况,它看起来像这样:

In your situation, it would look something like this:

public class CarsViewModel extends ViewModel {
    private CarRepository mCarRepository;
    private LiveData<List<Car>> mAllCars;
    private LiveData<List<Car>> mCarsFilteredByColor;
    private MutableLiveData<String> filterColor = new MutableLiveData<String>();
    
    public CarsViewModel (CarRepository carRepository) {
        super(application);
        mCarRepository= carRepository;
        mAllCars = mCarRepository.getAllCars();
        mCarsFilteredByColor = Transformations.switchMap(
            filterColor,
            color -> mCarRepository.getCarsByColor(color)
        );
    }
    
    LiveData<List<Car>>> getAllCars() { return mAllCars; }
    LiveData<List<Car>> getCarsFilteredByColor() { return mCarsFilteredByColor; }

    // When you call this function to set a different color, it triggers the new search
    void setFilter(String color) { filterColor.setValue(color); }
}

因此,当您从视图中调用 setFilter 方法时,更改filterColor LiveData将触发Transformation.switchMap,它将调用 mRepository.getCarsByColor(c))和然后使用查询结果更新mCarsFilteredByColor LiveData.因此,如果您在视图中观察此列表并且设置了不同的颜色,则观察者将接收到新数据.

So when you call the setFilter method from your view, changing the filterColor LiveData will triger the Transformation.switchMap, which will call mRepository.getCarsByColor(c)) and then update with the query's result the mCarsFilteredByColor LiveData. Hence, if you are observing this list in your view and you set a different color, the observer receive the new data.

这篇关于ViewModel-在运行时观察LiveData时更改方法的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆