java - 在Android中将两个parcelables列表写入parcel

标签 java android list parcelable parcel

我有对象联系方式:

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/

相关文章:

java - 如何将 Bitmap ArrayList 保存到文件

python - 如何在 Python 中将列表转换为带有空格的字符串?

java - 方法同步与对象同步有什么区别?

Java - java.io.NotSerializableException : net. sourceforge.jtds.jdbc.JtdsConnection

java - Spring MVC 国际化不起作用

android - 如何使用mediacodec在android中使用软件编解码器

带有 Camera API 的 Android 模拟器

python - 如何在 Flask-Jinja Python 中使用列表中的列表

java - 越来越多的线程导致我的应用程序滞后

android - 关闭应用程序而不是返回到之前的 Activity