如何把在Parcelable Location对象 [英] how to put a Location object in Parcelable

查看:106
本文介绍了如何把在Parcelable Location对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在实现Parcelable我的其他场馆对象的一个​​Location对象。如何在我的writeToParcel方法实现正确序列化呢?因此,这里的一些code:

 公共类场馆实现Parcelable {
    公共字符串ID;
    公共字符串名称;
    公共字符串的地址;
    公共场所的位置;
    公共字符串的城市;
    公共字符串状态;
    公共字符串的国家;
    公共字符串邮政code;

    @覆盖
    公众诠释describeContents(){
        // TODO自动生成方法存根
        返回0;
    }

    @覆盖
    公共无效writeToParcel(宗地递减,INT标志){
        desc.writeString(ID);
        desc.writeString(州);
        desc.writeString(名称);
        desc.writeString(地址);
        desc.writeString(邮政code);
        desc.writeString(国家);
        desc.writeString(市);
        //我仍然失踪的方式在这里连载我的地址对象
    }
}
 

解决方案

在这里,你有一个片段如何parcable对象序列化到自己的包裹。

 地点的位置;

公共无效writeToParcel(宗地递减,INT标志){
    location.writeToParcel(DESC,旗);
    / *在这里做你的其他包裹的东西* /
}

公共无效readFromParcel(包裹中){
    位置= Location.CREATOR.createFromParcel(中);
    / *在这里做你的其他包裹的东西* /
}
 

I have a Location object in one of my other Venue object that implements Parcelable. How do I serialize this correctly in my writeToParcel method implementation? So here's some code:

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
    }
}

解决方案

Here your have a snippet how to serialize parcable objects into your own parcel.

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 */
}

这篇关于如何把在Parcelable Location对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆