在具有嵌套非基本元素的对象中实现parcelable

时间:2012-10-17 22:25:27

标签: java android parcelable

我有一个

形式的对象
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(..)的类加载器是什么?

1 个答案:

答案 0 :(得分:1)

writeToParcel方法上你应该这样做:

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

dest方法中的参数writeToParcel有很多其他方法可以编写其他对象或原始类型。 要在dest对象中放置没有特定方法的其他对象,如果您尝试编写的对象实现writeParcelableParcelable,则应使用方法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

总结一下,我从未见过使用这种方法的任何情况。如果您想在解析包裹后释放资源,则使用该方法,但我没有在互联网上找到任何解释它的好材料。有可能你会让它返回0。 您可以找到它的Android文档here