可变列表<Any>添加所有 kotlin 整个数组列表添加为对象



以下是我的代码

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
)

我猜因为你的listtypeAny,它会考虑添加list实例也作为object,所以它作为listOf()直接添加

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

相关内容

  • 没有找到相关文章

最新更新