一般的对象序列化有两种方式Serializable和Parcelable,而Android项目推荐使用Parcelable进行序列化,给大家推荐一种AndroidStudio插件,可快速使你的对象实现序列化。
首先配置插件:
第一步,打开Setting
下载安装Android Parcelable code generator插件并重启工具。
第二步,开始生成序列化代码
在实体类中步骤如下:
选中的二级目录如下
点击生成序列化代码,结构如下:
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]; } }; }到此结束。