本文主要是介绍Android序列化Parcelable、Serializable,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在Android中序列化实体类的方法有两种:
一:Serializable
优点:
- 此方法特别简单,只需直接在实体类上声明Serializable接口
缺点:
- 效率没有Parcelable高
二:Parcelable,
优点:
- 序列化及反序列号效率高
缺点:
- 当数据将会被缓存进内存中的时候不建议这么做,容易导致数据丢失
使用方法:
先码上未被序列化的实体类
public class Student {private String name; private int age; private List<Book> books; class Book{private String bookName; private double bookPrice; public String getBookName() {return bookName; }public void setBookName(String bookName) {this.bookName = bookName; }public double getBookPrice() {return bookPrice; }public void setBookPrice(double bookPrice) {this.bookPrice = bookPrice; }}public String getName() {return name; }public void setName(String name) {this.name = name; }public int getAge() {return age; }public void setAge(int age) {this.age = age; }public List<Book> getBooks() {return books; }public void setBooks(List<Book> books) {this.books = books; } }
一:Serializable
只需在Student后加上Serializable如下public class Student implements Serializable{private String name; private int age; private List<Book> books; class Book{private String bookName; private double bookPrice; public String getBookName() {return bookName; }public void setBookName(String bookName) {this.bookName = bookName; }public double getBookPrice() {return bookPrice; }public void setBookPrice(double bookPrice) {this.bookPrice = bookPrice; }}public String getName() {return name; }public void setName(String name) {this.name = name; }public int getAge() {return age; }public void setAge(int age) {this.age = age; }public List<Book> getBooks() {return books; }public void setBooks(List<Book> books) {this.books = books; } }
二:Parcelable,
AndroidStudio建议使用插件 然后打开File -> Settings -> Pugins -> Browse Repositories 如下,输入 android parcelable code generator :
安装成功后需要重启AndroidStudio
Alt+ins键 然后点击Parcelable
全选之后点击OK
到了这一步有个坑,如果实体类中还有内部类的话到此还没有结束!
运行时会报AndroidRunTime error:Parcel:unable to marshal value异常所以我们还有一步没有完成
内部类Book也需要序列化
最后完整的Parcelable序列化的代码如下
public class Student implements Parcelable {private String name; private int age; private List<Book> books; public static class Book implements Parcelable {private String bookName; private double bookPrice; public String getBookName() {return bookName; }public void setBookName(String bookName) {this.bookName = bookName; }public double getBookPrice() {return bookPrice; }public void setBookPrice(double bookPrice) {this.bookPrice = bookPrice; }@Override public int describeContents() {return 0; }@Override public void writeToParcel(Parcel dest, int flags) {dest.writeString(this.bookName); dest.writeDouble(this.bookPrice); }public Book() {}protected Book(Parcel in) {this.bookName = in.readString(); this.bookPrice = in.readDouble(); }public static final Creator<Book> CREATOR = new Creator<Book>() {@Override public Book createFromParcel(Parcel source) {return new Book(source); }@Override public Book[] newArray(int size) {return new Book[size]; }}; }public String getName() {return name; }public void setName(String name) {this.name = name; }public int getAge() {return age; }public void setAge(int age) {this.age = age; }public List<Book> getBooks() {return books; }public void setBooks(List<Book> books) {this.books = books; }@Override public int describeContents() {return 0; }@Override public void writeToParcel(Parcel dest, int flags) {dest.writeString(this.name); dest.writeInt(this.age); dest.writeList(this.books); }public Student() {}protected Student(Parcel in) {this.name = in.readString(); this.age = in.readInt(); this.books = new ArrayList<Book>(); in.readList(this.books, Book.class.getClassLoader()); }public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {@Override public Student createFromParcel(Parcel source) {return new Student(source); }@Override public Student[] newArray(int size) {return new Student[size]; }}; }
这篇关于Android序列化Parcelable、Serializable的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!