【问题标题】:how to put a Location object in Parcelable如何将位置对象放入 Parcelable
【发布时间】:2011-11-13 15:26:02
【问题描述】:

我的另一个实现 Parcelable 的 Venue 对象中有一个 Location 对象。如何在我的 writeToParcel 方法实现中正确序列化它?所以这里有一些代码:

public class Venue implements Parcelable{
    public String id;
    public String name;
    public String address;
    public Location location;
    public String city;
    public String state;
    public String country;
    public String postalCode;

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel desc, int flags) {
        desc.writeString(id);
        desc.writeString(state);
        desc.writeString(name);
        desc.writeString(address);
        desc.writeString(postalCode);
        desc.writeString(country);
        desc.writeString(city);
        //I am still missing on a way to serialize my Location object here
    }
}

【问题讨论】:

  • 请编辑您的问题,使您的问题更清楚。到目前为止,您尝试过什么?
  • 编辑了一些代码示例,希望对您有所帮助

标签: java android


【解决方案1】:

这里你有一个 sn-p 如何将 parcable 对象序列化到你自己的包裹中。

Location location;

public void writeToParcel(Parcel desc, int flags) {
    location.writeToParcel(desc, flags);
    /* do your other parcel stuff here */
}

public void readFromParcel(Parcel in) {
    location=Location.CREATOR.createFromParcel(in);
    /* do your other parcel stuff here */
}

【讨论】:

  • 谢谢,但是列表/数组呢?您应该存储大小,还是应该如何处理它们?
【解决方案2】:

location = in.readParcelable(Location.class.getClassLoader()); 也可以。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 2014-10-15
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多