ArrayList<ArrayList<String>> 在 Parcelable 对象 kotlin
ArrayList<ArrayList<String>> in Parcelable object kotlin
需要对字符串数组的数组进行分割。对象是这样的
data class Foo (
@SerializedName("bar") val bar: ArrayList<ArrayList<String>>,
)
不一定非得是ArrayList。也可以用数组。
data class Foo (
@SerializedName("bar") val bar: Array<Array<String>>,
)
哪个更容易映射这个json数据
{
"bar": [
["a", "b"],
["a1", "b2", "c2"],
["a3", "b34", "c432"]
]
}
使用 kotlin experimental Parcelize
在使用 progaurd 编译时应用程序崩溃
"writeToParcel"怎么写,"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) {
// ?????
}
}
您不能直接为 List
的 List
创建 Parcelable
,因此一种解决方案是为您想要的 List
创建一个子class ] 作为 Parcelable
并将其作为最终列表类型。 如何? 查看下面的内容:
让我们首先创建我们的内部字符串列表 class,如下所示:
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>
parcelable,以便我们可以在任何端点使用它。
所以最终数据 class 将具有以下实现:
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.的简单实现,您可以根据自己的需要进行任意定制。
需要对字符串数组的数组进行分割。对象是这样的
data class Foo (
@SerializedName("bar") val bar: ArrayList<ArrayList<String>>,
)
不一定非得是ArrayList。也可以用数组。
data class Foo (
@SerializedName("bar") val bar: Array<Array<String>>,
)
哪个更容易映射这个json数据
{
"bar": [
["a", "b"],
["a1", "b2", "c2"],
["a3", "b34", "c432"]
]
}
使用 kotlin experimental Parcelize
在使用 progaurd 编译时应用程序崩溃
"writeToParcel"怎么写,"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) {
// ?????
}
}
您不能直接为 List
的 List
创建 Parcelable
,因此一种解决方案是为您想要的 List
创建一个子class ] 作为 Parcelable
并将其作为最终列表类型。 如何? 查看下面的内容:
让我们首先创建我们的内部字符串列表 class,如下所示:
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>
parcelable,以便我们可以在任何端点使用它。
所以最终数据 class 将具有以下实现:
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.的简单实现,您可以根据自己的需要进行任意定制。