首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我们如何决定使用写类型列表/ readTypedList还是写列表/ readList?

我们如何决定使用写类型列表/ readTypedList还是写列表/ readList?
EN

Stack Overflow用户
提问于 2017-07-23 16:52:25
回答 1查看 609关注 0票数 1

目前,我仍然想知道当我们为一个类实现Parcelable接口时,我们如何决定何时使用writeTypedList/ readTypedListwriteList/ readList

朗读

代码语言:javascript
运行
复制
// Using writeTypedList
parcel.writeTypedList(watchlistColumnTypes);

// Using writeList
parcel.writeList(watchlistColumnTypes);

代码语言:javascript
运行
复制
// Using readTypedList
watchlistColumnTypes = new ArrayList<>();
in.readTypedList(watchlistColumnTypes, WatchlistArrayRecyclerViewAdapter.ColumnType.CREATOR);


// Using readList
watchlistColumnTypes = new ArrayList<>();
in.readList(watchlistColumnTypes, WatchlistArrayRecyclerViewAdapter.class.getClassLoader());

对我来说都是可行的。但是,我不知道两者之间有什么区别,我们如何在两者之间作出选择呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-10 12:16:57

我认为经验法则是,我们应该尽可能使用writeTypedListreadTypedList,并在需要时使用writeListreadList

这是因为writeTypedListreadTypedList似乎具有更好的性能。

根据https://android.googlesource.com/platform/frameworks/base/+/1a008c1/core/java/android/os/Parcel.java#117

writeTypedObject(T,int)、writeTypedArray(T[],int)、writeTypedList(List)、readTypedObject(Parcelable.Creator)、createTypedArray(Parcelable.Creator)和createTypedArrayList(Parcelable.Creator)也提供了一种更有效的处理Parcelable的方法。这些方法不写入原始对象的类信息:相反,read函数的调用方必须知道预期的类型并传入适当的Parcelable.Creator,以正确构造新对象并读取其数据。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45267609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档