简体   繁体   English

Kotlin、Android - 如何转换 MutableLiveData <mutablelist< something> &gt; 到 LiveData <list< something> &gt; </list<></mutablelist<>

[英]Kotlin, Android - How to transform MutableLiveData<MutableList< something >> to LiveData<List< something >>

in the ViewModel I have MutableLiveData<MutableList<object>> and I want to create LiveData<List<object>> , but I don't know how to create List from MutableList .在 ViewModel 我有MutableLiveData<MutableList<object>>并且我想创建LiveData<List<object>> ,但我不知道如何从MutableList创建List

Here is what I'm trying to do:这是我正在尝试做的事情:

class AppViewModel : ViewModel() {

    private val _tasks = MutableLiveData<MutableList<Task>>(mutableListOf())
    val tasks: LiveData<List<Task>> = _tasks
}

(but I'm getting error Type mismatch. Required: LiveData<List> Found: MutableLiveData<MutableList> ) (但我收到错误类型不匹配。必需:LiveData<List> 找到:MutableLiveData<MutableList>

When I change the val tasks: LiveData<List<Task>> = _tasks to val tasks: LiveData<MutableList<Task>> = _tasks it works, but I want the List in tasks not to be editable .当我将val tasks: LiveData<List<Task>> = _tasksval tasks: LiveData<MutableList<Task>> = _tasks它可以工作,但我希望任务中的List不可编辑

LiveData is defined in Java, where there is no declaration-site variance. LiveData 在 Java 中定义,其中没有声明站点差异。 (If it were defined in Kotlin, they maybe would have made the type covariant out to avoid this problem.) To get around it, you can use use-site variance to make it covariant: (如果它是在 Kotlin 中定义的,他们可能会让类型协变out避免这个问题。)为了解决这个问题,您可以使用使用站点方差来使其协变:

val tasks: LiveData<out List<Task>> = _tasks

Covariant means you can't modify its contents from this reference, which is already true of read-only LiveData.协变意味着您不能从此引用中修改其内容,这对于只读 LiveData 已经是正确的。 This just makes it known to the compiler.这只是让编译器知道它。 Marking it covariant allows the the up-casting of the MutableList type to List type.将其标记为协变允许将 MutableList 类型向上转换为 List 类型。

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

相关问题 有没有更好的方法将私有 MutableLiveData 公开为 ViewModel 的 LiveData。 [安卓,科特林] - Is there a better way to expose private MutableLiveData as LiveData for an ViewModel. [Android, Kotlin] Android:将 LiveData 设置为 MutableLiveData - Android: Setting LiveData as MutableLiveData 如何在Kotlin中构建基于MutableList的List? - How to construct MutableList based List in Kotlin? Android Studio(Kotlin)如何将StringArray转换为MutableList - Android Studio (Kotlin) how to convert StringArray to MutableList 如何设置LiveData <List<customType> &gt;到MutableLiveData <List<customType> &gt;? - How to set LiveData<List<customType>> to MutableLiveData<List<customType>>? 如何在 Kotlin 中使用 MutableLiveData? - How to use MutableLiveData in Kotlin? Android cast MutableLiveData<mutableset> &gt; 到 LiveData<set> &gt;</set></mutableset> - Android cast MutableLiveData<MutableSet>> to LiveData<Set>> 更新 MutableLiveData 的 MutableList - Update MutableList of MutableLiveData 如何转换 LiveData <List<User> &gt; 到 LiveData <List<String> &gt; 在 ViewModel 中? - How to transform LiveData<List<User>> to LiveData<List<String>> in ViewModel? 如果 MutableList 为空,如何更改 TextView 的可见性? (安卓/科特林) - How to change visibility of a TextView if a MutableList is empty? (Android/Kotlin)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM