Android Kotlin-将映射结果转换为类型化arraylist
我很难将map的结果转换为它的模型类型Android Kotlin-将映射结果转换为类型化arraylist,android,kotlin,Android,Kotlin,我很难将map的结果转换为它的模型类型 val options = message.options.map {{ it.lastShare = user?.lastShare it.lastWatch = user?.lastWatch }} 我希望选项以typeArrayList的形式返回,但在internet上找不到任何内容。消息。选项属于列表类型。当我执行.map时,它返回列表单元>请尝试下一个代码: val options = message.
val options = message.options.map {{
it.lastShare = user?.lastShare
it.lastWatch = user?.lastWatch
}}
我希望选项以typeArrayList
的形式返回,但在internet上找不到任何内容。消息。选项属于列表类型。当我执行.map
时,它返回列表单元>
请尝试下一个代码:
val options = message.options.map {
it.lastShare = user?.lastShare
it.lastWatch = user?.lastWatch
it
}
或者在您的情况下,我认为您不需要使用map
函数,您只需使用forEach
更新列表中每个对象的属性即可:
message.options.forEach {
it.lastShare = user?.lastShare
it.lastWatch = user?.lastWatch
}
val optionsArrayList: ArrayList<Option> = arrayListOf(*message.options.toTypedArray())
只要写
val options = message.options.map {{
it.lastShare = user?.lastShare
it.lastWatch = user?.lastWatch
}}.toMutableList()
什么类型的选项是options
?options只是一个数据类@sergeyd您有什么错误吗?请删除代码中的一对大括号,然后查看它是否正确works@Sergey当然,因为我无法将其传递给accepsArrayList
的函数,您可以将函数参数的类型更改为List
?只需val options ArrayList=ArrayList(message.options)
val options = message.options.map {{
it.lastShare = user?.lastShare
it.lastWatch = user?.lastWatch
}}.toMutableList()