如何使界面Parcelable?

时间:2015-01-08 12:44:01

标签: android parcelable

如何使Interface对象成为Parcelable?

public interface Options{
    public String getAction();
    public String getType();

}


public class OptionsInfo implements Parcelable {

    private int duration;
    private ArrayList<Options> path;

    public OptionsInfo(int duration) {
        super();
        this.duration = duration;
        this.path = new ArrayList<Options>();
    }

    public ArrayList<Options> getPath() {
        return path;
    }

    public void setPath(ArrayList<Options> path) {
        this.path = path;
    }

     public void addToPath(Options object)
        {
            path.add(object);
        }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeInt(this.duration);
        dest.writeTypedList(this.path);

    }

    private OptionsInfo (Parcel in){

        path = new ArrayList<Options>();

        this.duration = in.readInt();

        in.readTypedList(this.path, Options.CREATOR);
    }

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

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

}

1 个答案:

答案 0 :(得分:-3)

public interface Options implements Parcelable {
public String getAction();
public String getType();

Creator<Options> getCreator();
}


public class OptionsInfo implements Options{

private int duration;
private ArrayList<Options> path;

public OptionsInfo(int duration) {
    super();
    this.duration = duration;
    this.path = new ArrayList<Options>();
}

public ArrayList<Options> getPath() {
    return path;
}

public void setPath(ArrayList<Options> path) {
    this.path = path;
}

 public void addToPath(Options object)
    {
        path.add(object);
    }

public int getDuration() {
    return duration;
}

public void setDuration(int duration) {
    this.duration = duration;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {

    dest.writeInt(this.duration);
    dest.writeTypedList(this.path);

}

private OptionsInfo (Parcel in){

    path = new ArrayList<Options>();

    this.duration = in.readInt();

    in.readTypedList(this.path, Options.CREATOR);
}

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

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

@Override
public Creator<Options> getCreator() {
    return CREATOR;
}
}