我正试图在android应用程序中编写一个可从activityA传递到activityB的可包裹数据对象.

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?

提前感谢您的帮助!

推荐答案

替换:

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

with:

arrayListOf<OrderedService>().apply {
    parcelIn.readList(this, OrderedService::class.java.classLoader)
}

Kotlin相关问答推荐

S使用MAP和ElseThrow的习惯用法是什么?

在Kotlin lambda的参数中如何指定函数类型?

Ktor 在 Heroku 上的 CORS 问题,但在本地没有

为什么 Kotlin 在将协变类型参数转换为不变类型参数时会产生 Unchecked Cast 警告?

返回 kotlin 中的标签和 lambda 表达式

如何使用成员引用在 Kotlin 中创建属性的分层路径

如何使用 Either monad 并避免嵌套 flatMap

Kotlin 中获取类简单名称的最佳实践

AIDL 中的 Parcelize 注释:Incompatible types: Object cannot be converted to MyCustomObject

Kotlin 代码是如何编译成原生代码的?

如何禁用智能投射突出显示 Kotlin?

Kotlin 中 lambda 表达式中的默认参数

Kotlinwhen表达式在使用主题时是否支持复合布尔表达式?

如何修复 ViewPager2 中的Design assumption violated错误?

如果我可以将 Flow 和 StateFlow 与生命周期范围 \ viewLifecycleOwner.lifecycleScope 一起使用,那么在 ViewModel 中使用 LiveData 有什么意义

在 Kotlin 中创建 Spinner 时,如何在 Fragment 中的旋转屏幕上修复指定为非空的参数为空?

使用导航组件在BottomNavigationView中传递参数

如果作为 RxJava Observable 提供,Kotlin 密封类子类需要强制转换为基类

Dagger +Kotlin 不注入

为什么在 Kotlin 中return可以返回一个return?