简体   繁体   English

MutableLiveData通过从ViewModel转换LiveData来公开

[英]MutableLiveData is exposed by casting LiveData from ViewModel

I used the code showen below to not expose the MutableLiveData in the main activity 我使用下面显示的代码在主要活动中不公开MutableLiveData

class CalculatorViewModel : ViewModel(){
 private val operation = MutableLiveData<String>()
    val stringOperation :LiveData<String>
        get() = operation
}

but I figured out a way to access the value of MutableLiveData via the LiveData getter and even change it and this is my code to do it: 但是我想出了一种通过LiveData getter访问MutableLiveData值甚至更改它的方法,这是我的代码:

(viewModel.stringOperation as MutableLiveData).value = ""

MutableLiveData is supposed to be used when you need to modify LiveData outside of your viewmodel. MutableLiveData应该被用来当你需要修改LiveData您的视图模型之外。 If you don't want it to get exposed from ViewModel, you must use LiveData. 如果您不希望它从ViewModel中公开,则必须使用LiveData。

If you look at LiveData class, you will notice that LiveData is an Abstract class , which means you have to extend it before you can use it. 如果查看LiveData类,您会注意到LiveData是Abstract类 ,这意味着您必须先对其进行扩展,然后才能使用它。 For cases such as yours, you would extend LiveData class. 对于像您这样的情况,您可以扩展LiveData类。 In that child class, you would for example call api and and update the value using private methods. 在该子类中,您将例如调用api并使用私有方法更新值。 So basically your LiveData class will be responsible for loading and updating the data. 因此,基本上,您的LiveData类将负责加载和更新数据。 Check out this link for an example: 查看此链接的示例:

https://developer.android.com/topic/libraries/architecture/livedata#extend_livedata https://developer.android.com/topic/libraries/architecture/livedata#extend_livedata

This is how to properly use LiveData class as per documentation: 这是根据文档正确使用LiveData类的方法:

public class StockLiveData extends LiveData<BigDecimal> {
    private StockManager stockManager;

    private SimplePriceListener listener = new SimplePriceListener() {
        @Override
        public void onPriceChanged(BigDecimal price) {
            setValue(price);
        }
    };

    public StockLiveData(String symbol) {
        stockManager = new StockManager(symbol);
    }

    @Override
    protected void onActive() {
        stockManager.requestPriceUpdates(listener);
    }

    @Override
    protected void onInactive() {
        stockManager.removeUpdates(listener);
    }
}

To sum up, Use MutableLiveData when you want to modify data outside of LiveData/ViewModel. 综上所述,当您要修改LiveData / ViewModel之外的数据时,请使用MutableLiveData。 For all other cases, provide your own implementation of LiveData. 对于所有其他情况,请提供自己的LiveData实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 androidx ViewModel MutableLiveData LiveData - androidx ViewModel MutableLiveData LiveData 我们如何在ViewModel中将LiveData从Room“分配”到MutableLiveData - How can we “assign” LiveData from Room to MutableLiveData, within ViewModel 具有来自 LiveData 的初始值的 MutableLiveData - MutableLiveData with initial value from LiveData 为什么将 MutableLiveData 转换为 LiveData 时会出现“无用转换”? - Why do i get “useless cast” when casting MutableLiveData to LiveData? 有没有更好的方法将私有 MutableLiveData 公开为 ViewModel 的 LiveData。 [安卓,科特林] - Is there a better way to expose private MutableLiveData as LiveData for an ViewModel. [Android, Kotlin] 来自房间的 LiveData 和 MutableLiveData 以显示错误消息 - LiveData from room and MutableLiveData to display error message 从 ViewModel 观察 LiveData - Observing LiveData from ViewModel 当在 MutableLiveData 变量上调用 postValue 时,公开的 LiveData 调度如何变化? - How does exposed LiveData dispatch changes when postValue is called on MutableLiveData variable? 如何从 Viewmodel 返回 Mutablelivedata hashmap - How to return Mutablelivedata hashmap from Viewmodel Android:将 LiveData 设置为 MutableLiveData - Android: Setting LiveData as MutableLiveData
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM