2

我有这堂课:

public class Foo implements Parcelable {
    private int id;
    private MyFoo myFoo
    private ForeignCollection<MyFoo2> myFoo2s;

    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(id);
        out.writeParcel(myFoo, flags);
        out.write //How can I write the ForeignCollection?
    } 

    public Foo(Parcel in) {
        id = in.readInt();
        myFoo = in.readParcelable(getClass().getClassLoader())
        myFoo2s = // How can I read the ForeignCollection?
    }

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

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

MyFoo 和 MyFoo2 类也实现 Parcelable,但 ForeignCollection 没有这样做。ForeignCollection是一个实现接口的类:Collection、CloseableIterable 和 Iterable。

我不能使用out.writeList,因为 ForeignCollection 没有实现 List 接口。

4

2 回答 2

2

看起来不可能把那个集合放到Parcel. 但是,您仍然可以基于正常的Serialization使用此答案的输入。

代码可能如下所示(代码来自上面的链接):

public static class SerializationUtility {
    /** Read the object from Base64 string. */
    static Object fromString(String s) throws IOException ,
            ClassNotFoundException {
        final byte [] data = Base64.decode(s, Base64.DEFAULT);
        final ObjectInputStream ois = new ObjectInputStream(
                new ByteArrayInputStream(data));
        final Object o  = ois.readObject();

        ois.close();

        return o;
    }

    /** Write the object to a Base64 string. */
    static  String toString(Serializable o) throws IOException {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final ObjectOutputStream oos = new ObjectOutputStream(baos);

        oos.writeObject(o);
        oos.close();
        return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
    }
}

public static class Foo implements Parcelable {
    private int id;
    private MyFoo myFoo;
    private ForeignCollection<MyFoo2> myFoo2s;

    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(id);
        out.writeParcelable(myFoo, flags);

        // Actually, this should be always true
        if (myFoo2s instanceof Serializable) {
            try {
                out.writeString(SerializationUtility.toString((Serializable) myFoo2s));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @SuppressWarnings("unchecked")
    public Foo(Parcel in) {
        id = in.readInt();
        myFoo = in.readParcelable(getClass().getClassLoader());
        try {
            myFoo2s = (ForeignCollection<MyFoo2>) SerializationUtility.fromString(in.readString());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

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

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

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

是的,它有一些缺点(例如未经检查的警告)并且它的速度会比正常速度慢Parcelable,但是它应该仍然比正常速度更快,Seriazable特别是如果MyFoo它非常复杂/很大。

于 2013-09-24T09:28:55.190 回答
1

here you can find an example that shows a solution to your problem:

  • Gift is the object I'm making Parcelable.
  • image: consider it as the only variable in this class and that it is a collection of Integer
  • method getImage: returns a collection of Integer (Collection)

You can apply this pattern to your objects that implement Collection

public void writeToParcel(Parcel parcel, int i) {

        List imageList = new ArrayList(getImage());
        parcel.writeList(imageList);
}

public Gift(Parcel source){

        image = source.readArrayList(Integer.class.getClassLoader());

}

P.S. do not use Collection.sort(imageList) in the writeToParcel method, because I would change the order of your collection reordering the elements in an ascending order.

Bests

于 2014-11-15T10:50:59.997 回答