日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

实现Parcelable接口

發(fā)布時間:2023/11/29 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实现Parcelable接口 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

1 官方例子

public class MyParcelable implements Parcelable {private int mData;public int describeContents() {return 0;}public void writeToParcel(Parcel out, int flags) {out.writeInt(mData);}public static final Parcelable.Creator<MyParcelable> CREATOR= new Parcelable.Creator<MyParcelable>() {public MyParcelable createFromParcel(Parcel in) {return new MyParcelable(in);}public MyParcelable[] newArray(int size) {return new MyParcelable[size];}};private MyParcelable(Parcel in) {mData = in.readInt();}}

實現(xiàn) Parcelable 的兩個方法,同時還要實現(xiàn)靜態(tài)變量CREATOR。 其他入門例子:

http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/

2 Parcel里面有帶classloader的方法可以直接填null,會使用默認(rèn)的classloader,但是用null會報異常,原因有待查。

3 Parcel的writeValue方法可以寫入Object類型的數(shù)據(jù)。

4 describeContents方法的用處 http://stackoverflow.com/questions/4778834/purpose-of-describecontents-of-parcelable-interface 主要就是用來區(qū)分子類的

父類抽象類 子類復(fù)寫describeContents方法

public XiangleTree createFromParcel(Parcel source) { int description = source.readInt(); switch (description) { case CATEGORY: Category category = new Category(); category.setId(source.readString()); category.setLevel(source.readString()); category.setName(source.readString()); category.setParentId(source.readString()); category.setHasCoupon(source.readString()); return category; case CIRCLE: Circle circle = new Circle(); circle.setId(source.readString()); circle.setLevel(source.readString()); circle.setName(source.readString()); circle.setParentId(source.readString()); circle.setHasCoupon(source.readString()); return circle; default: return null; } }

http://stackoverflow.com/questions/4076946/parcelable-where-when-is-describecontents-used

回答者還笑稱Parcelable 接口是個C++的程序員設(shè)計的,結(jié)果發(fā)現(xiàn)JAVA中沒有多重繼承。

5 ParcelableCONTENTS_FILE_DESCRIPTOR變量http://stackoverflow.com/questions/4076946/parcelable-where-when-is-describecontents-used

If you need to put FileDescriptor object into Parceable you should/must specify CONTENTS_FILE_DESCRIPTOR as return value of describeContents(),

i.e. by "special object" (indescribeContents()'s description) they really mean: FileDescriptor.

6 Parcelable 的PARCELABLE_WRITE_RETURN_VALUE變量用在writeToParcel (Parcel dest, int flags)方法。flags的值可以是0 or PARCELABLE_WRITE_RETURN_VALUE.

dest是某個方法的返回值such as "Parcelable someFunction()", "void someFunction(out Parcelable)", or "void someFunction(inout Parcelable)"

7 處理list:writeToParcel方法dest.writeTypedList(xxList) createFromParcel方法:source.readTypedList(xxList,XX.CREATOR),或者參考《樹形對象實現(xiàn)parcelable接口》

8 樹形對象實現(xiàn)Parcelable 見筆記 《樹形對象實現(xiàn)parcelable接口》

轉(zhuǎn)載于:https://my.oschina.net/dingbuoyi/blog/61903

總結(jié)

以上是生活随笔為你收集整理的实现Parcelable接口的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。