Java Firebase“;Parcelable在写入可序列化对象时遇到IOException;对象包含DocumentReference的ArrayList
我正试图将一个Java Firebase“;Parcelable在写入可序列化对象时遇到IOException;对象包含DocumentReference的ArrayList,java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,我正试图将一个用户对象传递给另一个活动,该对象包含FirestoreDocumentReference的Arraylist,当我启动该活动时,出现了此异常。 我没有使用Parcelable,因此您能否确认此错误是由于要传递的对象的复杂性造成的,并且我必须绝对使用Parcels而不是简单的可序列化的 感谢您的帮助您得到了这个错误,因为类没有实现parcelable或Serializable 如果需要序列化DocumentReference对象,则需要使用其方法获取描述文档在数据库中位置的字符串。要
用户
对象传递给另一个活动,该对象包含FirestoreDocumentReference的Arraylist,当我启动该活动时,出现了此异常。
我没有使用Parcelable,因此您能否确认此错误是由于要传递的对象的复杂性造成的,并且我必须绝对使用Parcels而不是简单的可序列化的
感谢您的帮助您得到了这个错误,因为类没有实现parcelable
或Serializable
如果需要序列化DocumentReference
对象,则需要使用其方法获取描述文档在数据库中位置的字符串。要将该路径字符串反序列化回DocumentReference
对象,请使用以下代码行:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
DocumentReference ref = rootRef.document(path);
有关方法,请参阅官方文档。公认的答案为我解决了问题。我只想与大家分享我的两种方法的实现,以便在运行中处理包裹!并且仍然将数组设置为ArrayList
好的,我需要修改我的用户POJO,用ArrayList而不是ArrayList来重新定义我的用户对象,谢谢。是的,因为模型类中的所有对象都应该是可序列化的。不客气!
private ArrayList<String> docRefToString(ArrayList<DocumentReference> products) {
ArrayList<String> newProducts = new ArrayList<String>();
for (DocumentReference product : products) {
newProducts.add(product.getPath());
}
return newProducts;
}
private ArrayList<DocumentReference> docStringToDoc(ArrayList<String> products) {
ArrayList<DocumentReference> newProducts = new ArrayList<DocumentReference>();
for (String product : products) {
newProducts.add(FirebaseFirestore.getInstance().document(product));
}
return newProducts;
}
private Request(Parcel in) {
...
ArrayList<String> strProducts = in.createStringArrayList();
this.products = docStringToDoc(strProducts);
...
}
@Override
public void writeToParcel(Parcel dest, int flags) {
...
dest.writeStringList(docRefToString(this.products));
...
}