国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

淺談Serializable與Parcelable

2019-11-09 14:27:45
字體:
供稿:網(wǎng)友

Android中使用Intent來啟動(dòng)Android的組件,而且在啟動(dòng)Android中的組件的時(shí)候可以使用Intent攜帶并傳遞數(shù)據(jù)。如:

Intent intent=new Intent(MainActivity.this,AnotherActivity.class); intent.putExtra("per_age",20); startActivity(intent);

但是當(dāng)使用Intent的putExtra()方法來傳遞數(shù)據(jù)的時(shí)候,只能傳遞基本類型的數(shù)據(jù),無法傳遞對象類型的數(shù)據(jù)。為了保證Intent可以傳遞對象類型的數(shù)據(jù)我們可以采取兩種方式,實(shí)體類實(shí)現(xiàn)Serializable接口和實(shí)體類實(shí)現(xiàn)Parcelable接口。 Serializable方式: Serializable方式指的是序列化方式,而 java的對象序列化是指將那些實(shí)現(xiàn)了Serializable接口的對象轉(zhuǎn)換成一個(gè)字符序列,并能夠在以后將這個(gè)字節(jié)序列完全恢復(fù)為原來的對象。 Serializable是一個(gè)聲明式的接口,沒有任何需要實(shí)現(xiàn)的抽象方法,因此當(dāng)需要將對象序列化的時(shí)候,只需要實(shí)體類簡單的實(shí)現(xiàn)一下接口就行。

/** * Created by BeautifulSoup on 2017/2/9. */public class Person implements Serializable{ PRivate String name; private Integer age; public Person() { } public Person(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '/'' + ", age=" + age + '}'; }}

實(shí)際操作:

Person person=new Person("BeautifulSoup",20); Intent intent=new Intent(MainActivity.this,AnotherActivity.class); intent.putExtra("per",person); startActivity(intent);

在目標(biāo)Activity中得到傳遞的數(shù)據(jù):

Person person=(Person)getIntent().getParcelableExtra("per");

此種方式十分簡單,但是效率不高,因此實(shí)際操作中我們通常使用Parcelable方式。

Parcelable方式: Parcelable方式的原理是將一個(gè)完整的對象進(jìn)行分解,從而將完整的對象分解成intent可以傳遞的基本類型的數(shù)據(jù)。 基本使用的方式就是實(shí)體類實(shí)現(xiàn)Parcelable接口,重寫接口未實(shí)現(xiàn)的方法。 其中describeContents方法的返回值一般是0,writeToParcel方法中將對象中的數(shù)據(jù)寫出。 此外,我們還需要在實(shí)體類中提供一個(gè)CREATOR的常量。在匿名類中將寫出的字段依次的讀入即可。

/** * Created by BeautifulSoup on 2017/2/9. */public class User implements Parcelable { private String name; private Integer age; private String address; private String phone; protected User() { } public User(String name, Integer age, String address, String phone) { this.name = name; this.age = age; this.address = address; this.phone = phone; } //省略getter與setter... public static final Creator<User> CREATOR = new Creator<User>() { @Override public User createFromParcel(Parcel in) { User user=new User(); user.name=in.readString(); user.age=in.readInt(); user.address=in.readString(); user.phone=in.readString(); return user; } @Override public User[] newArray(int size) { return new User[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel parcel, int i) { parcel.writeString(name); parcel.writeInt(age.intValue()); parcel.writeString(address); parcel.writeString(phone); } @Override public String toString() { return "User{" + "name='" + name + '/'' + ", age=" + age + ", address='" + address + '/'' + ", phone='" + phone + '/'' + '}'; }} User user=new User("BeautifulSoup",20,"山東","17811111111"); Intent intent=new Intent(MainActivity.this,AnotherActivity.class); intent.putExtra("user",user); startActivity(intent);

在目標(biāo)Activity中得到傳遞的數(shù)據(jù):

User user=(User)getIntent().getParcelableExtra("user");
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 长治县| 武定县| 吐鲁番市| 蓝田县| 常宁市| 南部县| 尼勒克县| 永登县| 芦山县| 荣成市| 阿坝县| 峨边| 莲花县| 专栏| 漳州市| 罗平县| 桦川县| 都匀市| 开封市| 辰溪县| 毕节市| 甘泉县| 青岛市| 福安市| 蒙城县| 清远市| 亚东县| 卢氏县| 松溪县| 中西区| 井陉县| 洱源县| 勃利县| 汉寿县| 栾城县| 含山县| 彭山县| 卫辉市| 抚远县| 拉孜县| 霍邱县|