2017-08-31 4 views
1

メンバー変数がArrayList<>であるクラスがあります。どちらのクラスもparcelableを実装しています。私は、他のクラスへの参照を持つクラスを完成させる方法についていません。ここでカスタムパーセル化可能オブジェクトでパーセル化

は私が持っているものである。このクラスはArrayList<Section>あるsectionsと呼ばれる値を持っているか

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) 
     } 
    } 
} 

注意してください。その変数をパーセル可能にする必要がありますが、それは機能しません。

参考のため、ここには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行です。

ありがとうございました。

答えて

1

まずソリューション

@Suppress("UNCHECKED_CAST") 
constructor(parcel: Parcel): this(parcel.readString(), parcel.readString(), parcel.readString(), parcel.readArrayList(Tab::class.java.classLoader) as ArrayList<Section>) 

第二の溶液

constructor(parcel: Parcel): this(parcel.readString(), parcel.readString(), parcel.readString(), ArrayList<Section>()){ 
    parcel.readTypedList(sections, Section.CREATOR) 
} 
+0

これは素晴らしいです。ありがとうございました! – Alex

関連する問題