阅读Vector <customobject> parcelable </customobject>

时间:2014-12-22 17:11:47

标签: java vector parcelable

使用parcelables获取参数时遇到问题。当我调用getIntent()。getExtras()。getParcelable(&#34; map&#34;);我有一个问题,因为Vector有问题。

我有两个可分类的课程:

public class Point implements Parcelable{
private double x;
private double y;
private double z;

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

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

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

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

public void readFromParcel(Parcel in){
    x = in.readDouble();
    y = in.readDouble();
    z = in.readDouble();
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeDouble(x);
    dest.writeDouble(y);
    dest.writeDouble(z);
}   
}

和地图:

    public class Map implements Parcelable{
        private Point start;
        private Point finish;
        private java.util.Vector <Point> travel;


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

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

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

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

    public void readFromParcel(Parcel in){
        start = in.readParcelable(Point.class.getClassLoader());
        finish = in.readParcelable(Point.class.getClassLoader());
        Point[] travel = (Point[]) in.readParcelableArray(Point.class.getClassLoader());
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(start, flags);
        dest.writeParcelable(finish, flags);
        dest.writeParcelableArray(travel.toArray(new Point[travel.size()]), flags);
}
}

然后,我怎么才能接受旅行?我总是在readParcelableArray中遇到问题。

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

当几乎不可能找到解决方案时,问题就是这个解决方案的基础。

就我而言,解决方案包括使用List而不是Vector。

然后,在Map类中,我改变了方法readFromParceo和writeToParcel,如下所示:

public void readFromParcel(Parcel in){
    start = in.readParcelable(Point.class.getClassLoader());
    finish = in.readParcelable(Point.class.getClassLoader());
    travel = in.readArrayList(Point.class.getClassLoader());
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeParcelable(start, flags);
    dest.writeParcelable(finish, flags);
    dest.writeList(travel);
}

我希望将来可以帮助别人。