本文介绍了ArrayList<ArrayList<String>>在 Parcelable 对象 kotlin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要对字符串数组的数组进行分块.对象是这样的

Array of arrays of strings needs to be parcelized. The object is like so

data class Foo (
    @SerializedName("bar") val bar: ArrayList<ArrayList<String>>,
)

它并不完全需要是 ArrayList.也可以使用数组.

It doesn't exactly need to be ArrayList. Array can also used.

data class Foo (
    @SerializedName("bar") val bar: Array<Array<String>>,
)

哪个更容易映射这个json数据

Whichever easier is ok to map this json data

{
  "bar": [
    ["a", "b"],
    ["a1", "b2", "c2"],
    ["a3", "b34", "c432"]
  ]
}

使用 kotlin 实验性 Parcelize 在使用 progaurd 编译时导致应用程序崩溃

Using kotlin experimental Parcelize crashes the app when it's compiled with progaurd

如何在writeToParcel"中写入并在constructor"中读取?

How is it written in "writeToParcel" and read in "constructor"?

data class Foo (
  @SerializedName("bar") val bar: ArrayList<ArrayList<String>>,
) : Parcelable {

  constructor(source: Parcel) : this(
     // ?????
  )

  override fun writeToParcel(dest: Parcel, flags: Int) = with(dest) {
    // ?????
  }

}

推荐答案

不能直接为ListList直接创建Parcelable,因此一种解决方案是将所需的 List 的一个子类设为 Parcelable 并将其作为最终列表类型.如何?看看下面:

You can't directly create Parcelable for List of List directly, so one solution is to make one subclass of your desired List as Parcelable and take it as you final list type. How? check out below :

让我们首先创建我们的内部 String 类列表,如下所示:

class StringList() : ArrayList<String>(), Parcelable {
    constructor(source: Parcel) : this() {
        source.createStringArrayList()
    }

    override fun describeContents() = 0

    override fun writeToParcel(dest: Parcel, flags: Int) {
        dest.writeStringList(this@StringList)
    }

    companion object {
        @JvmField
        val CREATOR: Parcelable.Creator<StringList> = object : Parcelable.Creator<StringList> {
            override fun createFromParcel(source: Parcel): StringList = StringList(source)
            override fun newArray(size: Int): Array<StringList?> = arrayOfNulls(size)
        }
    }
}

我们在这里所做的是创建了我们的 ArrayList 可分块,以便我们可以在任何端点使用它.

What we've done here is created our ArrayList<String> parcelable so that we can use it at any endpoint.

所以最终的数据类将具有以下实现:

So final data class would be having following implementation :

data class Foo(@SerializedName("bar") val bar: List<StringList>) : Parcelable {
    constructor(source: Parcel) : this(
        source.createTypedArrayList(StringList.CREATOR)
    )

    override fun describeContents() = 0

    override fun writeToParcel(dest: Parcel, flags: Int) = with(dest) {
        writeTypedList(bar)
    }

    companion object {
       @JvmField
       val CREATOR: Parcelable.Creator<Foo> = object : Parcelable.Creator<Foo> {
            override fun createFromParcel(source: Parcel): Foo = Foo(source)
            override fun newArray(size: Int): Array<Foo?> = arrayOfNulls(size)
       }
    }
}

注意:这是基于O.P.的简单实现,您可以根据您的要求进行任何自定义.

Note: It's simple implementation based on O.P., you can make any customization based on your requirement.

这篇关于ArrayList<ArrayList<String>>在 Parcelable 对象 kotlin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 23:12