How to parcel List<Int> with kotlin
Asked Answered
L

2

13

I want to pass a data class (that contains list of int as a property) to other activity through Bundle and therefore i need to add Parcelable implementation to my data class. any idea about how to parcel this property?

data class Test(val id: Long, val files: List<Int>?) : Parcelable {

constructor(parcel: Parcel) : this(
        parcel.readLong(),
        TODO("files"))

override fun writeToParcel(parcel: Parcel, flags: Int) {
    parcel.writeLong(id)
}

override fun describeContents(): Int {
    return 0
}

companion object CREATOR : Parcelable.Creator<Test> {
    override fun createFromParcel(parcel: Parcel): Test {
        return Test(parcel)
    }

    override fun newArray(size: Int): Array<Test?> {
        return arrayOfNulls(size)
    }
}
Leathaleather answered 21/1, 2018 at 19:49 Comment(0)
C
16

You can write a list of integers as int[]:

parcel.writeIntArray(files.toIntArray())

Make sure you use the same data structure when reading back:

files = parcel.createIntArray().toList()

You could make it more efficient using extension functions by skipping the array representation:

parcel.writeIntList(files)
files = parcel.createIntList()

fun Parcel.writeIntList(input:List<Int>) {
    writeInt(input.size) // Save number of elements.
    input.forEach(this::writeInt) // Save each element.
}

fun Parcel.createIntList() : List<Int> {
    val size = readInt()
    val output = ArrayList<Int>(size)
    for (i in 0 until size) {
        output.add(readInt())
    }
    return output
}
Castlereagh answered 21/1, 2018 at 20:43 Comment(1)
Hi, I have same problem i have followed your way but still getting an error, will you please help me #58977396Hippie
P
18

Kotlin's @Parcelize annotation handles lists; you just need to make sure the list items implement the Parcelable interface as well:

@Parcelize
data class MyDataClass(
    val items: List<MyListItem>?
) : Parcelable

@Parcelize
data class MyListItem(
    var type: String?,
    var itemId: Long = 0
) : Parcelable
Penetrate answered 10/7, 2020 at 16:34 Comment(2)
This also needsid 'kotlin-parcelize' inside plugins at build.gradle (app).Paulettepauley
This actually worked. I had to extend my data class with parcelable and put @Parcelize on top of it.Cormick
C
16

You can write a list of integers as int[]:

parcel.writeIntArray(files.toIntArray())

Make sure you use the same data structure when reading back:

files = parcel.createIntArray().toList()

You could make it more efficient using extension functions by skipping the array representation:

parcel.writeIntList(files)
files = parcel.createIntList()

fun Parcel.writeIntList(input:List<Int>) {
    writeInt(input.size) // Save number of elements.
    input.forEach(this::writeInt) // Save each element.
}

fun Parcel.createIntList() : List<Int> {
    val size = readInt()
    val output = ArrayList<Int>(size)
    for (i in 0 until size) {
        output.add(readInt())
    }
    return output
}
Castlereagh answered 21/1, 2018 at 20:43 Comment(1)
Hi, I have same problem i have followed your way but still getting an error, will you please help me #58977396Hippie

© 2022 - 2024 — McMap. All rights reserved.