简体   繁体   中英

Writing a List to Parcelable?

I have a custom object that has private List<AnotherCustomObject> mList; . Note that AnotherCustomObject implements Parcelable .

However if I want to write mList to dest in writeToParcel() and then read it back later, is this the correct way:

dest.writeList(mList);

and then in the read method

in.readList(mList, ClassLoader.getSystemClassLoader());

Correct?

请尝试使用必要的类加载器。

in.readList(mList, AnotherCustomObject.getClass().getClassLoader());

Your List<AnotherCustomObject> mList can be read using the readTypedList(List<T> list, Creator<T> c) method and written to using the writeTypedList(List<T> val) method.

In your writeToParcel method you would write the following:

dest.writeTypedList(mList);

And then in the constructor which creates an instance from a Parcel object you can put:

in.readTypedList(mList, AnotherCustomObject.CREATOR);  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM