通常的對象序列化有兩種方式Serializable和Parcelable,而Android項目推薦使用Parcelable進行序列化,給你們推薦一種AndroidStudio插件,可快速使你的對象實現序列化。ide
首先配置插件:工具
第一步,打開Setting
this
下載安裝Android Parcelable code generator插件並重啓工具。spa
第二步,開始生成序列化代碼.net
在實體類中步驟以下:插件
選中的二級目錄以下3d
點擊生成序列化代碼,結構以下:code
public class UserDao implements Parcelable { private int id; private String name; private String sex; private int age; private String phone; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(this.id); dest.writeString(this.name); dest.writeString(this.sex); dest.writeInt(this.age); dest.writeString(this.phone); } public UserDao() { } protected UserDao(Parcel in) { this.id = in.readInt(); this.name = in.readString(); this.sex = in.readString(); this.age = in.readInt(); this.phone = in.readString(); } public static final Parcelable.Creator<UserDao> CREATOR = new Parcelable.Creator<UserDao>() { public UserDao createFromParcel(Parcel source) { return new UserDao(source); } public UserDao[] newArray(int size) { return new UserDao[size]; } }; }到此結束。
本文分享 CSDN - 秦川小將。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。對象