Kotlin - 类型不匹配所需的集合找到列表

jo *_* jo 8 android kotlin

我需要附加两个list,但它告诉我:

type mismatch required collection found list
Run Code Online (Sandbox Code Playgroud)

就像下面这样:

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 3

两个变化:

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)
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 请解释你的答案 (2认同)