[英]Writing a List to Parcelable?
我有一個自定義對象,具有private List<AnotherCustomObject> mList;
。 需要注意的是AnotherCustomObject
實現Parcelable
。
但是,如果我想要寫mList
到dest
在writeToParcel()
然后讀回來后,這才是正確的方法:
dest.writeList(mList);
然后在read方法中
in.readList(mList, ClassLoader.getSystemClassLoader());
正確?
請嘗試使用必要的類加載器。
in.readList(mList, AnotherCustomObject.getClass().getClassLoader());
可以使用readTypedList(List<T> list, Creator<T> c)
方法讀取List<AnotherCustomObject> mList
readTypedList(List<T> list, Creator<T> c)
並使用writeTypedList(List<T> val)
方法寫入。
在writeToParcel
方法中,您將編寫以下內容:
dest.writeTypedList(mList);
然后在從Parcel
對象創建實例的構造函數中,您可以放置:
in.readTypedList(mList, AnotherCustomObject.CREATOR);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.