本文介绍了LiveDataReactiveStreams 到 MutableLiveData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将 LiveDataReactiveStreams 中的值发布到 MutableLiveData?我想实现到 Switch(视图)的双向数据绑定,并将检查"值从数据库传递到 MutableLiveData 以及从 UI 传递.LiveDataReactiveStreams 仅返回不可变的 LiveData.
How can I publish values from LiveDataReactiveStreams to MutableLiveData? I would like to implement two-way data binding to Switch (view) and pass "checked" value from database to MutableLiveData and from UI as well.LiveDAtaReactiveStreams returns immutable LiveData only.
//ViewModel
public final MutableLiveData<Boolean> switchChecked = new MutableLiveData<>();
LiveData<Boolean> data = LiveDataReactiveStreams.fromPublisher(/* Flowable from DB */); //??
//xml
<Switch
...
android:checked="@={viewModel.switchChecked}"
/>
推荐答案
Try MediatorLiveData
//ViewModel
public final MediatorLiveData<Boolean> switchChecked = new MediatorLiveData<>();
public MyViewModel() {
...
switchChecked.addSource(LiveDataReactiveStreams.fromPublisher(/* Flowable from DB */), value -> {
switchChecked.setValue(value);
});
...
}
这篇关于LiveDataReactiveStreams 到 MutableLiveData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!