ArrayList< ArrayList< String>>在可包裹物体科特林中 [英] ArrayList<ArrayList<String>> in Parcelable object kotlin

查看:115
本文介绍了ArrayList< ArrayList< String>>在可包裹物体科特林中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字符串数组的数组需要打包.对象就像这样

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"中并在构造函数"中读取的?

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<String>可打包文件将被创建,以便我们可以在任何端点使用它.

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&lt; ArrayList&lt; String&gt;&gt;在可包裹物体科特林中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆