Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android LiveData<;列表<;儿童>&燃气轮机;不是LiveData的子类<;列表<;家长>&燃气轮机;_Android_Kotlin - Fatal编程技术网

Android LiveData<;列表<;儿童>&燃气轮机;不是LiveData的子类<;列表<;家长>&燃气轮机;

Android LiveData<;列表<;儿童>&燃气轮机;不是LiveData的子类<;列表<;家长>&燃气轮机;,android,kotlin,Android,Kotlin,我有一个抽象的ViewModel类,我们称之为AbstractListViewModel。 它有一个类型为MutableLiveData的itemsList。JSONObject有两个子对象:JSONChildOne和JSONChildTwo。我想重写AbstractViewModel子级中的属性,以键入MutableLiveData 我试图在子类中重写MutableLiveData AbstractListViewModel: abstract val itemsList : MutableL

我有一个抽象的ViewModel类,我们称之为AbstractListViewModel。 它有一个类型为MutableLiveData>的itemsList。JSONObject有两个子对象:JSONChildOne和JSONChildTwo。我想重写AbstractViewModel子级中的属性,以键入MutableLiveData>

我试图在子类中重写MutableLiveData>

AbstractListViewModel:

abstract val itemsList : MutableLiveData<List<JSONObject>>
override val itemsList =  MutableLiveData<List<JSONChildOne>>()
abstract val itemsList:MutableLiveData
ChildOneListViewModel:

abstract val itemsList : MutableLiveData<List<JSONObject>>
override val itemsList =  MutableLiveData<List<JSONChildOne>>()
override val itemsList=MutableLiveData()

属性类型为“MutableLiveData>”,它不是重写的子类型,
List
不扩展
List
(有关原因的讨论,请参阅)

您应该在
AbstractViewModel
中使用泛型参数,例如:

abstract class AbstractViewModel<T: JSONObject> {
    abstract val itemsList : MutableLiveData<List<T>>
}

class ChildOneListViewModel : AbstractViewModel<JSONChildOne> {
    override val itemsList : MutableLiveData<List<JSONChildOne>>
}
抽象类AbstractViewModel{
抽象val itemsList:MutableLiveData
}
类ChildOneListViewModel:AbstractViewModel{
覆盖val itemsList:MutableLiveData
}

可能的副本也值得一读:谢谢,但问题是我不知道如何在Kotlin中使用它