1

以下是我的代码

var items : MutableList<Any> = arrayListOf()
        items.add(TeacherDetails(it?.photo,it?.firstName,it?.lastName,it?.level))
        items.add(TeacherBio(it?.bio))
        items.add(TitleAccreditations(getString(R.string.acreditations)))
        items.add(SessionsTitle(it?.firstName + getString(R.string.apostrophe) + getString(
                        R.string.sessions)))
        items.addAll(listOf(it?.classes ?: arrayListOf()))
        items.add(IntroVideo(it?.introVideo))
        items.addAll(it?.teachingAccreditations?.split("\n")?.map { Accreditation(
            it
        ) }?: emptyList())

问题在下一行,它将整个列表添加为对象而不是单个项目。

items.addAll(listOf(it?.classes ?: arrayListOf()))

以下是我的模型

data class Teacher(

@field:SerializedName("firstName")
val firstName: String? = null,

@field:SerializedName("lastName")
val lastName: String? = null,

@field:SerializedName("teacherId")
val teacherId: String? = null,

@field:SerializedName("introVideo")
val introVideo: String? = null,

@field:SerializedName("level")
val level: String? = null,

@field:SerializedName("teachingAccreditations")
val teachingAccreditations: String? = null,

@field:SerializedName("classes")
val classes: List<ClassesItem?>? = null,

@field:SerializedName("photo")
val photo: String? = null,

@field:SerializedName("bio")
val bio: String? = null
)
4

1 回答 1

2

我猜因为你typelistis Any,它会考虑将listinstance 也添加为 an object,所以它listOf()只是直接添加

items.addAll(it?.classes?.filterNotNull()?: arrayListOf())
于 2021-02-06T07:12:11.633 回答