我有对象联系方式:
public class Contact implements Parcelable
具有属性:
private List<EmailAttribute> contact_emails_attributes = new ArrayList<EmailAttribute>();
private List<PhoneAttribute> contact_phones_attributes = new ArrayList<PhoneAttribute>();
EmailAttribute 和 PhoneAttribute 实现了 Parcelable。
如何将联系人的属性写入包裹并读取它们?我一直在寻找解决方案,但我没有找到如何将不同的 Parcelables 写入 Parcel 的解决方案。 谢谢。
最佳答案
您可以使用方法 Parcel.writeTypedList(List) 来完成此操作和 Parcel.readTypedList(List, Creator)像这样:
从包裹中读取:
public Contact(Parcel in){
contact_emails_attributes = in.readTypedList(contact_emails_attributes, EmailAttribute.CREATOR);
contact_phones_attributes = in.readTypedList(contact_phones_attributes, PhoneAttribute.CREATOR);
}
将其写入包裹:
public void writeToParcel(Parcel dest, int flags) {
dest.writeTypedList(contact_emails_attributes);
dest.writeTypedList(contact_phones_attributes);
}
关于java - 在Android中将两个parcelables列表写入parcel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23404067/