MutableList<Any> addAll kotlin 整个 arraylist 添加为对象
MutableList<Any> addAll kotlin entire arraylist added as object
以下是我的代码
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
)
我猜是因为 list
的 type
是 Any
,它会考虑将 list
实例也添加为 object
,所以改为listOf()
直接加就行了
items.addAll(it?.classes?.filterNotNull()?: arrayListOf())
以下是我的代码
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
)
我猜是因为 list
的 type
是 Any
,它会考虑将 list
实例也添加为 object
,所以改为listOf()
直接加就行了
items.addAll(it?.classes?.filterNotNull()?: arrayListOf())