Android Kotlin-找到类型不匹配的必需集合列表
我需要附加两个Android Kotlin-找到类型不匹配的必需集合列表,android,kotlin,Android,Kotlin,我需要附加两个列表,但它会告诉我: type mismatch required collection found list 就像贝娄说的: val list: List<Cohort> = ArrayList() private fun fillFromDao() { val notesObserver: Observer<ArrayList<Cohort?>?>? = Observer { cohort: ArrayList<
列表
,但它会告诉我:
type mismatch required collection found list
就像贝娄说的:
val list: List<Cohort> = ArrayList()
private fun fillFromDao() {
val notesObserver: Observer<ArrayList<Cohort?>?>? =
Observer { cohort: ArrayList<Cohort?>? ->
list.toMutableList().addAll(cohort)
}
if (notesObserver != null) {
otherDialogFragmentViewModel.fetchIsFree()?.observe(this, notesObserver)
otherDialogFragmentViewModel.fetchHasCertificate()?.observe(this, notesObserver)
}
}
val list:list=ArrayList()
私人娱乐{
val notesObserver:观察者=
观察者{队列:ArrayList?->
list.toMutableList().addAll(队列)
}
if(notesObserver!=null){
otherDialogFragmentViewModel.fetchIsFree()?.otherDialogFragmentViewModel.fetchIsFree().Other观察(此,notesObserver)
otherDialogFragmentViewModel.fetchHasCertificate()?.otherDialogFragmentViewModel.fetchHasCertificate().Other观察(此,notesObserver)
}
}
list
类型的变量list
,但是notesObserver
值类型是ArrayList
:因此notesObserver
值可以有可为空的元素
因此,list
变量应接受可空值,或者notesObserver
不应具有可空值。两个更改:
val list: List<Cohort?> = ArrayList() ----> add '?' after Cohort
private fun fillFromDao() {
val notesObserver: Observer<ArrayList<Cohort?>?>? =
Observer { cohort: ArrayList<Cohort?>? ->
list.toMutableList().addAll(cohort!!) ----> add '!!' after Cohort
}
if (notesObserver != null) {
otherDialogFragmentViewModel.fetchIsFree()?.observe(this, notesObserver)
otherDialogFragmentViewModel.fetchHasCertificate()?.observe(this, notesObserver)
}
}
val list:list=ArrayList()--->在队列后添加“?”
私人娱乐{
val notesObserver:观察者=
观察者{队列:ArrayList?->
list.toMutableList().addAll(队列!!)--->在队列后添加“!!”
}
if(notesObserver!=null){
otherDialogFragmentViewModel.fetchIsFree()?.otherDialogFragmentViewModel.fetchIsFree().Other观察(此,notesObserver)
otherDialogFragmentViewModel.fetchHasCertificate()?.otherDialogFragmentViewModel.fetchHasCertificate().Other观察(此,notesObserver)
}
}
请解释您的答案Kotlin是一种类型安全的语言,所以请键入队列和队列?是一种不同的类型。如果你观察一个队列列表?值,您需要一个适当的容器来容纳它们-所以您需要列表