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

时间:2019-03-04 13:28:11

标签: android kotlin parcelable

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

当列表为空时,

writeTypedList和createTypedArrayList不起作用

2 个答案:

答案 0 :(得分:1)

非常简单,仅当列表不为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);
}

答案 1 :(得分:0)

  

如何发送包含自定义对象的自定义对象列表?

您需要为创建的链接中的每个自定义对象实现while(!read.eof()) { if(i==4){ i=0; } read>>b; if(i==0){ names.push_back(b); } if(i==1){ last_name.push_back(b); } if(i==2){ absences.push_back(b); } if(i==3){ unatoned.push_back(b); } i++; } int a = atoi(absences[0].c_str());

Parcelable interface

dest.writeTypedList(customList) // in write to parcel 

请记住,不要在链接中实现可组合界面时遗漏任何对象