gpt4 book ai didi

android - 如何使用 Parcelable 传递可为空的对象列表

转载 作者:行者123 更新时间:2023-11-29 23:17:52 24 4
gpt4 key购买 nike

我想传递一个自定义对象,该对象具有其他自定义对象的可为空列表。我该如何解决这个问题。我正在使用 Kotlin

当列表为空时,writeTypedList 和 createTypedArrayList 不起作用

最佳答案

非常简单,您只需要读取不为 null 的列表即可。

dest.writeByte(list == null ? (byte)0 : (byte)1)
if(list != null) {
dest.writeInt(list.size());
dest.writeTypedList(list);
}

然后

boolean hasList = in.readByte() > 0;
if(hasList) {
int size = in.readInt();
List<MyObj> list = new ArrayList<>(size);
in.readTypedList(list, MyObj.CREATOR);
}

关于android - 如何使用 Parcelable 传递可为空的对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54984301/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com