android - 打包包含带有自定义对象的集合的模型

标签 android ormlite parcelable parcel

我有这门课:

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是一个实现以下接口(interface)的类:Collection、CloseableIterable 和 Iterable。

我无法使用out.writeList,因为ForeignCollection没有实现List接口(interface)。

最佳答案

看起来不可能将该集合放入 Parcel 中。但是,您仍然可以使用 this answer 中的输入基于正常的序列化

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

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 相当复杂/庞大。

关于android - 打包包含带有自定义对象的集合的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18867519/

相关文章:

android - 如何在 Android webview 中制作一个正方形大小的 HTML 表格填充屏幕

android - 如何在 Ormlite 中将 ForeignCollection 字段放入游标

android - 使用 Robolectric 和 ORMLite 进行测试时使用模拟数据库

Android:错误:不能从静态上下文引用非静态类型变量 T

android - 如何在 Android Lollipop 版本的 Parcelable 类中添加/保留 Class<?> 对象

android-contentprovider - 如何编码/解码 ContentValues 以将泛型类型插入 ContentProvider?

android - 如何修复 android SQLite 中的插入数据?

java - 如何通过单击 SearchView 打开新 Activity

android - 我如何让我的代码正确地从零开始工作?

android - 为什么不将 OrmLiteSqliteOpenHelper 绑定(bind)到应用程序而不是 Activity ?