【问题标题】:Implements parcelable in an object with nested non-primitive elements在具有嵌套非原始元素的对象中实现 parcelable
【发布时间】:2012-10-08 07:16:58
【问题描述】:

我在表单中有一个对象

public class Car implements Serializable, Parcelable {

    private static final long serialVersionUID = 1L;

    String name;
    String description;
    String brand;
    int speed;
    int brake;
    int asset;
    ArrayList<Uri> images;

    public Car(String name, String description, String brand, int speed,
            int brake, int asset, ArrayList<Uri> images) {
        super();
        this.name = name;
        this.description = description;
        this.brand = brand;
        this.speed = speed;
        this.brake = brake;
        this.asset = asset;
        this.images = images;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getBrake() {
        return brake;
    }

    public void setBrake(int brake) {
        this.brake = brake;
    }

    public int getAsset() {
        return asset;
    }

    public void setAsset(int asset) {
        this.asset = asset;
    }

    public ArrayList<Uri> getImages() {
        return images;
    }

    public void setImages(ArrayList<Uri> images) {
        this.images = images;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + asset;
        result = prime * result + brake;
        result = prime * result + ((brand == null) ? 0 : brand.hashCode());
        result = prime * result
                + ((description == null) ? 0 : description.hashCode());
        result = prime * result + ((images == null) ? 0 : images.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + speed;
        return result;
    }

    @Override
    public String toString() {
        return "Car [name=" + name + ", description=" + description
                + ", brand=" + brand + ", speed=" + speed + ", brake=" + brake
                + ", asset=" + asset + ", images=" + images + "]";
    }

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

    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub

    }




}

但是我不明白如何实现

public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub

}

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

implements Parcelable 添加到类后,Eclipse 需要

在 Android 网站上,示例还显示了一种方法

  public static final Parcelable.Creator<MyParcelable> CREATOR
             = new Parcelable.Creator<MyParcelable>() {
         public MyParcelable createFromParcel(Parcel in) {
             return new MyParcelable(in);
         }

让我的整个类(以及所有嵌套对象为 ArrayList)“Parcelable” 的正确方法是什么?

按照示例我到达构造函数

public Car(Parcel in) {
   name= in.readString();
 description= in.readString();
speed=in.readInt();
...

images=in.readArrayList(??????????????????);
}

要传入readArrayList(..) 的类加载器是什么?

【问题讨论】:

    标签: java android parcelable


    【解决方案1】:

    writeToParcel 方法上你应该这样做:

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(brand); //To write String
    }
    

    writeToParcel 方法中的参数dest 有很多其他方法可以编写其他对象或原始类型。 要将其他没有特定方法的对象放在dest 对象中,如果您尝试编写的对象实现了Parcelable,则应使用方法writeParcelable,如果您尝试编写的对象实现了writeSerializable Serializable.

    之后,当您恢复 Parceable 对象时,您应该使用:

    private void readFromParcel(Parcel in) {
        brand = in.readString(); //To read String
    }
    

    您必须按照您编写它们的顺序从 in 对象中读取值。

    这段代码:

    public static final Parcelable.Creator<Car> CREATOR = new Parcelable.Creator<Car>() {
             public Car createFromParcel(Parcel in) {
                 return new Car(in);
             }
    }
    

    需要调用readFromParcel 方法。您应该在您的类中添加一个构造函数,以便从上面的代码中调用。 构造函数应如下所示:

    public Car(Parcel in) {
       readFromParcel(in);
    }
    

    关于describeContents的方法,Stackoverflow上有一个很好的答案,here

    总之,我从未见过任何使用这种方法的案例。该方法用于Parcel解析后释放资源,但我在网上没有找到任何解释它的好材料。很有可能你会保持它返回 0。 你可以找到它的 Android 文档here

    【讨论】:

    • 我正在关注示例,但在公共 Car(Parcel in) {...} 现在我到达了 images=in.readArrayList(Class loader) 并且我不知道我在做什么应该通过。对于原语只是 name=in.readString() speed=in.readInt()etc.
    • 你应该使用writeSerializable来写,readSerializable来读。可以看到所有方法here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多