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.
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);
}
});
By using this code, I recieve list of "red" cars and populate RecyclerView with the List.
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.