Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin parcelable and arrayList of parcelables

Tags:

I am trying to write a parcelable data object to pass to from activityA to activityB in my android application.

My object is passing with all the data, except my arraylist of the class Available Service

data class AvailableService(val id: Int,                         val name: String,                         val description: String,                         val price: Double,                         val currency: String,                         val imageUrl: String) : Parcelable {  companion object {     @JvmField @Suppress("unused")     val CREATOR = createParcel { AvailableService(it) } }  protected constructor(parcelIn: Parcel) : this(parcelIn.readInt(),         parcelIn.readString(),         parcelIn.readString(),         parcelIn.readDouble(),         parcelIn.readString(),         parcelIn.readString())  override fun writeToParcel(dest: Parcel?, flags: Int) {     dest?.writeInt(id)     dest?.writeString(name)     dest?.writeString(description)     dest?.writeDouble(price)     dest?.writeString(currency)     dest?.writeString(imageUrl) }  override fun describeContents() = 0 } 

above is the available serviceClass, next I have Trip, which holds an arraylist of AvailableService.. I observed this in debug, it is successfully writing the arraylist, for some reason I have an issue with reading the data.

data class Trip(val id: String,             val status: String,             val orderedServices: ArrayList<OrderedService>) : Parcelable {  companion object {     @JvmField @Suppress("unused")     val CREATOR = createParcel { Trip(it) } }  protected constructor(parcelIn: Parcel) : this(parcelIn.readString(),         parcelIn.readString(),         arrayListOf<OrderedService>().apply {             parcelIn.readArrayList(OrderedService::class.java.classLoader)         } )  override fun writeToParcel(dest: Parcel?, flags: Int) {     dest?.writeString(id)     dest?.writeString(status)     dest?.writeList(orderedServices) }  override fun describeContents() = 0 } 

in case someone wonders what's the fun of the CREATOR does, code below:

inline fun <reified T : Parcelable> createParcel(     crossinline createFromParcel: (Parcel) -> T?): Parcelable.Creator<T> =     object : Parcelable.Creator<T> {         override fun createFromParcel(source: Parcel): T? = createFromParcel(source)         override fun newArray(size: Int): Array<out T?> = arrayOfNulls(size)     } 

again, writing succeeds, but reading fails, I get an empty arraylist.. I think that part is the faulty one:

arrayListOf<OrderedService>().apply {         parcelIn.readArrayList(OrderedService::class.java.classLoader)     } 

is there a different way to read/write arraylist? am I writing it wrong? reading it wrong?

thanks in advance for any help!

like image 484
JozeRi Avatar asked Aug 02 '17 11:08

JozeRi


People also ask

What is Parcelable kotlin?

Save. In Android, to pass the data from one activity to another activity, we use the Parcelable. The kotlin-parcelize plugin provides a Parcelable implementation generator. The Android's Parcelable is an interface on which, the values can be written and read from a Parcel.

How do I send Parcelable intent kotlin?

android studio will show you an error. Simple move your cursor to "People" and hit alt+enter. It should now show the option to generate a Parcelable implementation.

How do I make a kotlin data class Parcelable?

There are 3 ways you can make your class Parcelable: Implementing the Parcelable interface and defining the serialization yourself (The traditional way) Using Android Studio plugins, like Android Parcelable code generator. Using annotation-based libraries, like Parceler.

What is the use of Parcelable in Android?

Parcelable and Bundle objects are intended to be used across process boundaries such as with IPC/Binder transactions, between activities with intents, and to store transient state across configuration changes.


1 Answers

Replace:

arrayListOf<OrderedService>().apply {     parcelIn.readArrayList(OrderedService::class.java.classLoader) } 

with:

arrayListOf<OrderedService>().apply {     parcelIn.readList(this, OrderedService::class.java.classLoader) } 
like image 188
Lid Avatar answered Sep 16 '22 20:09

Lid