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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android中传递对象的三种方法

發布時間:2023/12/18 Android 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android中传递对象的三种方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Android中,Activity和Fragment之間傳遞對象,可以通過將對象序列化并存入Bundle或者Intent中進行傳遞,也可以將對象轉化為JSON字符串,進行傳遞。

序列化對象可以使用Java的Serializable的接口、Parcelable接口。轉化成JSON字符串,可以使用Gson等庫。

1.Serializable

Model

  • public?class?Author?implements?Serializable{
  • ????private?int?id;
  • ?
  • ????private?String?name;
  • ?
  • ????//...
  • }
  • public?class?Book?implements?Serializable{
  • ????private?String?title;
  • ????private?Author?author;
  • ????//...
  • }
  • 傳遞數據

  • ??Book?book=new?Book();?
  • ??book.setTitle("Java編程思想");?
  • ??Author?author=new?Author();?
  • ??author.setId(1);?
  • ??author.setName("Bruce?Eckel");?
  • ??book.setAuthor(author);?
  • ??Intent?intent=new?Intent(this,SecondActivity.class);?
  • ??intent.putExtra("book",book);?
  • ??startActivity(intent);
  • 接收數據

  • ?Book?book=?(Book)?getIntent().getSerializableExtra("book");
  • ?Log.d(TAG,"book?title->"+book.getTitle());
  • ?Log.d(TAG,"book?author?name->"+book.getAuthor().getName());
  • 2.轉化為JSON字符串

    Model

  • public?class?Author{
  • ????private?int?id;
  • ?
  • ????private?String?name;
  • ?
  • ????//...
  • }
  • public?class?Book{
  • ????private?String?title;
  • ????private?Author?author;
  • ????//...
  • }
  • 傳遞數據

  • Book?book=new?Book();
  • book.setTitle("Java編程思想");
  • Author?author=new?Author();
  • author.setId(1);
  • author.setName("Bruce?Eckel");
  • book.setAuthor(author);
  • Intent?intent=new?Intent(this,SecondActivity.class);
  • intent.putExtra("book",new?Gson().toJson(book));
  • startActivity(intent);
  • 接收數據

  • String?bookJson=getIntent().getStringExtra("book");
  • Book?book=new?Gson().fromJson(bookJson,Book.class);
  • Log.d(TAG,"book?title->"+book.getTitle());
  • Log.d(TAG,"book?author?name->"+book.getAuthor().getName());
  • 3.使用Parcelable

    實現Parcelable接口需要實現兩個方法

    • describeContents方法。內容接口描述,默認返回0就可以;

    • writeToParcel方法。將傳遞的數據打包到Parcel容器中。

    除了要實現這兩個方法還必須創建一個Parcelable.Creator接口的實例,用于讀取Parcel容器中的數據

    Model

  • public?class?Author?implements?Parcelable{
  • ????private?int?id;
  • ?
  • ????private?String?name;
  • ?
  • ????//setter?&?getter...
  • ?
  • ????@Override
  • ????public?int?describeContents()?{
  • ?
  • ????????return?0;
  • ????}
  • ?
  • ????@Override
  • ????public?void?writeToParcel(Parcel?dest,?int?flags)?{
  • ????????//該方法將類的數據寫入外部提供的Parcel中.即打包需要傳遞的數據到Parcel容器保存,
  • ????????//?以便從parcel容器獲取數據
  • ????????dest.writeString(name);
  • ????????dest.writeInt(id);
  • ?
  • ????}
  • ????public?static?final?Creator<Author>?CREATOR=new?Creator<Author>()?{
  • ????????@Override
  • ????????public?Author?createFromParcel(Parcel?source)?{
  • ????????????//從Parcel容器中讀取傳遞數據值,封裝成Parcelable對象返回邏輯層。
  • ????????????Author?author=new?Author();
  • ????????????author.setName(source.readString());
  • ????????????author.setId(source.readInt());
  • ????????????return?author;
  • ????????}
  • ?
  • ????????@Override
  • ????????public?Author[]?newArray(int?size)?{
  • ????????????//創建一個類型為T,長度為size的數組,僅一句話(return?new?T[size])即可。方法是供外部類反序列化本類數組使用。
  • ????????????return?new?Author[size];
  • ????????}
  • ????};
  • }
  • ?

  • public?class?Book?implements?Parcelable{
  • ????private?String?title;
  • ????private?Author?author;
  • ????//setter?&?getter...
  • ?
  • ????@Override
  • ????public?int?describeContents()?{
  • ????????return?0;
  • ????}
  • ?
  • ????@Override
  • ????public?void?writeToParcel(Parcel?dest,?int?flags)?{
  • ????????dest.writeString(title);
  • ????????dest.writeParcelable(author,flags);
  • ????}
  • ????public?static?final?Creator<Book>?CREATOR=new?Creator<Book>()?{
  • ????????@Override
  • ????????public?Book?createFromParcel(Parcel?source)?{
  • ????????????Book?book=new?Book();
  • ????????????book.setTitle(source.readString());
  • ????????????book.setAuthor(source.<Author>readParcelable(Author.class.getClassLoader()));
  • ????????????return?book;
  • ????????}
  • ?
  • ????????@Override
  • ????????public?Book[]?newArray(int?size)?{
  • ????????????return?new?Book[0];
  • ????????}
  • ????};
  • }
  • 傳遞數據

  • Book?book=new?Book();
  • book.setTitle("Java編程思想");
  • Author?author=new?Author();
  • author.setId(1);
  • author.setName("Bruce?Eckel");
  • book.setAuthor(author);
  • Intent?intent=new?Intent(this,SecondActivity.class);
  • intent.putExtra("book",book);
  • startActivity(intent);
  • 接收數據

  • Book?book=getIntent().getParcelableExtra("book");
  • Log.d(TAG,"book?title->"+book.getTitle());
  • Log.d(TAG,"book?author?name->"+book.getAuthor().getName());
  • 4.性能分析

    經過測試,我們得到下圖的效果

    可以看出,通過轉換為字符串的速度是最慢的。Seralizable次之,Parcelable比Seralizable快10倍。所以從性能上考 慮,我們必定優先選擇Parcelable。但是Parcelable有大量重復的模板代碼,如何簡化這些操作,將是下面主要講解的內容。

    5.簡化Parcel操作

    如果你使用android Studio 可以通過安裝android-parcelable-intellij-plugin插件,或者自己配置模板進行操作。

    5.1 parceler

    除了上面的操作,還有大量的第三方庫來簡化Parcelable操作。當然使用這些庫也許會降低Parcelable的性能。Parceler就是這樣一個庫。

    Parceler使用非常簡單,在定義Model時用@Parcel進行注解,在傳遞數據的時候使用Parcels的wrap方法來包裝成一個Parcelable對象。獲取數據時用Parcels的unwrap方法來獲取對象。

    Model

  • @Parcel
  • ?
  • public?class?Author?{
  • ?
  • ???int?id;
  • ?
  • ????String?name;
  • ?
  • ????//setter?&?getter...
  • }
  • @Parcel
  • public?class?Book?{
  • ????String?title;
  • ????Author?author;
  • ????//setter?&?getter
  • }
  • 傳遞對象

  • Book?book=new?Book();
  • book.setTitle("Java編程思想");
  • Author?author=new?Author();
  • author.setId(1);
  • author.setName("Bruce?Eckel");
  • book.setAuthor(author);
  • Intent?intent=new?Intent(this,SecondActivity.class);
  • intent.putExtra("book",?Parcels.wrap(book));
  • startActivity(intent);
  • 接收對象

  • Book?book=?Parcels.unwrap(getIntent().getParcelableExtra("book"));
  • Log.d(TAG,"book?title->"+book.getTitle());
  • Log.d(TAG,"book?author?name->"+book.getAuthor().getName());
  • 除了Parceler之外,還有如auto-parcel,ParcelableCodeGenerator,ParcelableGenerator等第三方庫,這里我將不進行講解,有興趣的朋友,可以自行研究。

    轉自:Malinkang's Blog-Android中傳遞對象?

    總結

    以上是生活随笔為你收集整理的Android中传递对象的三种方法的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。