我有一个类,其成员变量类型为ArrayList<>
。这两个类都实现了parcelable。我坚持如何完成引用另一个类的类。
这就是我所拥有的:
data class Tab (val name: String, val title: String, val color: String, val sections: ArrayList<Section>) : Parcelable {
constructor(parcel: Parcel) : this(
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readTypedList<Section>(sections, Section.CREATOR))
override fun writeToParcel(dest: Parcel?, flags: Int) {
dest?.writeString(name)
dest?.writeString(title)
dest?.writeString(color)
dest?.writeTypedList<Section>(sections)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<Tab> {
override fun createFromParcel(parcel: Parcel): Tab {
return Tab(parcel)
}
override fun newArray(size: Int): Array<Tab?> {
return arrayOfNulls(size)
}
}
}
请注意,此类的sections
值为ArrayList<Section>
。我需要将该变量写入parcelable,但它不起作用。
供参考,这是Section
类。我认为这个没关系:
data class Section(val type: String, val text: String, val imageName: String) : Parcelable {
constructor(parcel: Parcel) : this(
parcel.readString(),
parcel.readString(),
parcel.readString())
override fun writeToParcel(dest: Parcel?, flags: Int) {
dest?.writeString(type)
dest?.writeString(text)
dest?.writeString(imageName)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<Section> {
override fun createFromParcel(parcel: Parcel): Section {
return Section(parcel)
}
override fun newArray(size: Int): Array<Section?> {
return arrayOfNulls(size)
}
}
}
这是失败的readTypedList和writeTypedList行。
感谢您的帮助。
答案 0 :(得分:1)
第一个解决方案
<template>
<button class="btn btn-md btn-success the-button" @click="makeItHappen()">Sender: {{what}}</button>
</template>
<script>
export default {
name: "the-button",
props: ["what"],
methods: {
makeItHappen: function(){
EventBus.$emit("somethingHappened", this.what)
}
}
}
</script>
第二个解决方案
@Suppress("UNCHECKED_CAST")
constructor(parcel: Parcel): this(parcel.readString(), parcel.readString(), parcel.readString(), parcel.readArrayList(Tab::class.java.classLoader) as ArrayList<Section>)