【问题标题】:LiveDataReactiveStreams to MutableLiveDataLiveDataReactiveStreams 到 MutableLiveData
【发布时间】:2019-06-07 11:08:45
【问题描述】:

如何将值从 LiveDataReactiveStreams 发布到 MutableLiveData?我想实现双向数据绑定到 Switch(视图)并将“检查”值从数据库传递到 MutableLiveData 和 UI。 LiveDAtaReactiveStreams 仅返回不可变的 LiveData。

//ViewModel
public final MutableLiveData<Boolean> switchChecked = new MutableLiveData<>();

LiveData<Boolean> data = LiveDataReactiveStreams.fromPublisher(/* Flowable from DB */); //??


//xml
<Switch
  ...
  android:checked="@={viewModel.switchChecked}"
/>

【问题讨论】:

    标签: android android-databinding android-livedata


    【解决方案1】:

    试试MediatorLiveData

    //ViewModel
    public final MediatorLiveData<Boolean> switchChecked = new MediatorLiveData<>();
    
    public MyViewModel() {
        ...
    
        switchChecked.addSource(LiveDataReactiveStreams.fromPublisher(/* Flowable from DB */), value -> {
            switchChecked.setValue(value);
        });
    
        ...
    }
    
    

    【讨论】:

    • 谢谢!我浪费了几个小时来寻找解决方案,最后你帮助了我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    相关资源
    最近更新 更多