【问题标题】:write two lists of parcelables into parcel in Android在Android中将两个parcelables列表写入parcel
【发布时间】:2014-05-01 07:58:14
【问题描述】:

我有对象联系人:

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。

如何将联系人的属性写入包裹并读取它们?我一直在寻找解决方案,但我没有找到如何将不同的包裹写入包裹的解决方案。 谢谢。

【问题讨论】:

    标签: java android list parcelable parcel


    【解决方案1】:

    你可以使用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);
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-05
      • 2016-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多