【问题标题】:Incompatible types with LiveData of a child as a LiveData of a parent与子 LiveData 作为父 LiveData 不兼容的类型
【发布时间】:2019-04-09 21:56:28
【问题描述】:

我想使用 MutableLiveData 来观察来自 ViewModel 的一些数据。问题是我使用子类和父类,并且与 LiveData 存在一些不兼容。 我想在 Kotlin 中做的一个例子:

import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.ViewModel

class Test : ViewModel() {

    abstract class Parent(protected var id: Int)

    class ChildFirst(id: Int) : Parent(id)

    class ChildSecond(id: Int) : Parent(id)

    var childFirst : MutableLiveData<ChildFirst> = MutableLiveData<ChildFirst>()

    var childSecond : MutableLiveData<ChildSecond> = MutableLiveData<ChildSecond>()

    var shouldManageFirstChild = true

    fun returnCorrectChild(): MutableLiveData<Parent> {
        if (shouldManageFirstChild) {
            return childFirst //won't compile in Android Studio (Type mismatch)
        } else {
            return childSecond as MutableLiveData<Parent> //compile and work with a warning in AndroidStudio (Unchecked cast)
        }
    }
}

这里是Java:

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

public class Test extends ViewModel {
    class Parent {
        protected int mId;

        Parent(int id) {
            mId = id;
        }
    }

    class ChildFirst extends Parent {
        ChildFirst(int id) {
            super(id);
        }
    }

    class ChildSecond extends Parent {
        ChildSecond(int id) {
            super(id);
        }
    }

    MutableLiveData<ChildFirst> childFirst = new MutableLiveData <ChildFirst>();

    MutableLiveData<ChildSecond> childSecond = new MutableLiveData <ChildSecond>();
    boolean shouldManageFirstChild = true;

    MutableLiveData<Parent> returnCorrectChild(){
        if (shouldManageFirstChild) {
            return childFirst; //won't compile in Android Studio (Incompatible type)
        } else {
            return (MutableLiveData<Parent>) childSecond;   //won't compile in Android Studio (Inconvertible cast)
        }
    }
}

如您所见,问题在于编译器不认为 MutableLiveData 和 MutableLiveData 的类型相同。

在 Kotlin 中,我可以将孩子的 LiveData 转换为父母。即使有警告,代码也会按预期运行:我可以观察到 MutableLiveData.

更糟糕的是,在 Java 中,即使使用强制转换也无法编译。

所以这是我的问题:

  • 为什么我不能将孩子的 LiveData 用作父母的 LiveData?这是 LiveData 的意图吗?

  • 使用 kotlin 'as' 进行转换是否会产生一些最终后果?

【问题讨论】:

    标签: android android-studio android-livedata


    【解决方案1】:

    为什么我不能将孩子的 LiveData 用作父母的 LiveData ? 这是 LiveData 的意图吗?

    在 java 中答案很简单:Java 中的泛型类型是不变的,这意味着 List&lt;String&gt; 不是 List&lt;Object&gt; 的子类型(引自 kotlin 文档 here

    它们是用 kotlin 'as' 铸造它的一些最终后果吗? ?

    Kotlin 只是在进行编译时警告,但在运行时发生的情况是您的检查仅针对非泛型部分。这就是为什么,我认为(如果有人比我更了解,请解释一下!我对此很感兴趣),你可以在 Kotlin 中做到这一点。

    为了更好地解释在使用泛型类型时在 kotlin(以及 Java 中)中发生的事情,我建议您阅读有关泛型的完整 kotlin 文档here 甚至还有很多关于 Java 泛型转换的文章,例如 this one

    编辑: 解决您的问题的一种方法可能是: 声明一个单独的实时数据并以如下简单的方式对待孩子:

    val liveParent = MutableLiveData<Parent>()
    val childFirst = ChildFirst()
    val childSecond = ChildSecond()
    

    然后在调用returnCorrectChild()时在liveParent中返回正确的孩子

    fun returnCorrectChild() {
        if (shouldManageFirstChild) {
            liveParent.value = firstChild
        } else {
            liveParent.value = secondChild
        }
    }
    

    【讨论】:

    • 感谢您的解释和解决方案示例
    【解决方案2】:

    您可以重构您的 returnCorrectChild() 函数以返回一个泛型类型,然后检查返回的类型是否是您的 ViewModel 类之外的 ChildFirst 或 ChildSecond 的实例

    类似:

      fun returnCorrectChild(): MutableLiveData<*> {
            if (shouldManageFirstChild) {
                return childFirst
            } else {
                return childSecond
            }
        }
    

    在您的 viewModel 类之外:

    yourLiveDataInstance.observe(this@YourActivity, Observer {
                when(it){
                    is ChildFirst -> {
                        // do stuff for ChildFirst
                    }
                    is ChildSecond ->{
                        // do stuff for ChildSecond
                    }
                }
            })
    

    【讨论】:

    • 该解决方案的问题:我只想在观察者中使用 Parent,并且当我不需要时添加一个演员(理论上)你真的回答了我的问题。跨度>
    猜你喜欢
    • 1970-01-01
    • 2019-07-17
    • 2020-01-22
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多