gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 08:55:58 24 4
gpt4 key购买 nike

我有这门课:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com