Android可包裹复制对象

Android可包裹复制对象,android,parcelable,parcel,Android,Parcelable,Parcel,假设我有两个类:ChildModel和ParentModel public class ParentModel implements Parcelable { private int id; private String value; private ArrayList<ChildModel> childs; public int getId() { return id; } public void setId(in

假设我有两个类:ChildModel和ParentModel

public class ParentModel implements Parcelable {
    private int id;
    private String value;
    private ArrayList<ChildModel> childs;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String toString() {
        return String.format("%s %d %s", "[PARENT]", id, value);
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public ArrayList<ChildModel> getChilds() {
        return childs;
    }

    public void setChilds(ArrayList<ChildModel> childs) {
        this.childs = childs;
    }


    public ParentModel() {
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.id);
        dest.writeString(this.value);
    }

    protected ParentModel(Parcel in) {
        this.id = in.readInt();
        this.value = in.readString();
    }

    public static final Creator<ParentModel> CREATOR = new Creator<ParentModel>() {
        public ParentModel createFromParcel(Parcel source) {
            return new ParentModel(source);
        }

        public ParentModel[] newArray(int size) {
            return new ParentModel[size];
        }
    };
}
public类ParentModel实现可包裹{
私有int-id;
私有字符串值;
私人ArrayList childs;
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串toString(){
返回字符串。格式(“%s%d%s”,“[PARENT]”,id,value);
}
公共字符串getValue(){
返回值;
}
公共void设置值(字符串值){
这个值=值;
}
公共数组列表getChilds(){
返回儿童;
}
public void setChilds(ArrayList childs){
this.childs=childs;
}
公共父模型(){
}
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
dest.writeInt(此.id);
dest.writeString(此值);
}
受保护的父模型(地块中){
this.id=in.readInt();
this.value=in.readString();
}
公共静态最终创建者=新创建者(){
公共父模型createFromParcel(地块源){
返回新的父模型(源);
}
公共父模型[]新数组(整数大小){
返回新的ParentModel[大小];
}
};
}

public类ChildModel实现可包裹{
私有int-id;
私有字符串值;
私人父母模式父母;
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串getValue(){
返回值;
}
公共void设置值(字符串值){
这个值=值;
}
公共ParentModel getParent(){
返回父母;
}
public void setParent(ParentModel parent){
this.parent=parent;
}
公共字符串toString(){
返回String.format(“%s%d%s”,“[CHILD]”,id,value);
}
公共儿童模型(){
}
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
dest.writeInt(此.id);
dest.writeString(此值);
dest.writeparceable(this.parent,0);
}
受保护的子模型(包裹中){
this.id=in.readInt();
this.value=in.readString();
this.parent=in.readParcelable(ParentModel.class.getClassLoader());
}
公共静态最终创建者=新创建者(){
公共子模型createFromParcel(地块源){
返回新的子模型(源);
}
公共子模型[]新数组(整数大小){
返回新的ChildModel[大小];
}
};
}
将2
ChildModel
ArrayList
传递到第二个活动中时,虽然在活动1中,这2个
ChildModel
具有相同的
ParentModel
,但这里这2个具有2个不同的
ParentModel
(对于
ParentModel
中的每个字段,值相同)。有人知道怎么解决这个问题吗

更一般地说,是否有任何机制允许在将同一对象写入包两次时,它只会指向
包中的同一内存

public class ChildModel implements Parcelable {
    private int id;
    private String value;
    private ParentModel parent;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public ParentModel getParent() {
        return parent;
    }

    public void setParent(ParentModel parent) {
        this.parent = parent;
    }

    public String toString(){
        return String.format("%s %d %s", "[CHILD]", id, value);
    }

    public ChildModel() {
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.id);
        dest.writeString(this.value);
        dest.writeParcelable(this.parent, 0);
    }

    protected ChildModel(Parcel in) {
        this.id = in.readInt();
        this.value = in.readString();
        this.parent = in.readParcelable(ParentModel.class.getClassLoader());
    }

    public static final Creator<ChildModel> CREATOR = new Creator<ChildModel>() {
        public ChildModel createFromParcel(Parcel source) {
            return new ChildModel(source);
        }

        public ChildModel[] newArray(int size) {
            return new ChildModel[size];
        }
    };
}