簡體   English   中英

將列表寫入Parcelable?

[英]Writing a List to Parcelable?

我有一個自定義對象,具有private List<AnotherCustomObject> mList; 需要注意的是AnotherCustomObject實現Parcelable

但是,如果我想要寫mListdestwriteToParcel()然后讀回來后,這才是正確的方法:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM