我想传递一个自定义对象,该对象具有其他自定义对象的可空列表。如何绕过这个。我正在使用Kotlin
当列表为空时,writeTypedList和createTypedArrayList不起作用
非常简单,只需要在列表不为空时才需要读取列表。
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);
}
如何发送自定义对象列表,而自定义对象又由自定义对象组成?
您需要为您创建的链接中的每个自定义对象实现Parcelable interface
。
dest.writeTypedList(customList) // in write to parcel
和
this.customList = in.createTypedArrayList(CustomObject.CREATOR) // in the constructor which receives (Parcel in) as parameter
请记住,不要错过在链接中实现parcelable接口的任何对象。