实现Parcelable接口,Writing a list of parcelable objects to a parcelable object

来源:互联网 发布:通信工程用什么软件 编辑:程序博客网 时间:2024/06/03 22:40

问题回答参考:http://stackoverflow.com/questions/19955482/writing-a-list-of-parcelable-objects-to-a-parcelable-object
关于Parcelable转自简书博客:http://www.jianshu.com/p/b1fcff2728bd

什么是Parcelable接口

作用:给类设计的接口,类一旦实现这个接口,该类的对象即可进行一个“从对象存储为Parcel , 从Parcel读取为对象”的一个读写过程。
目的:实现序列化
原因:
序列化,为了:
1) 永久保存对象,保存对象的字节到本地文件中
2) 通过序列化对象在网络中传递对象
3) 通过序列化在进程间传递对象
与Serializable对比:
1)在使用Intent传递数据时:

Serializable: Bundle.putSerializable(Key, 实现了Serializable的对象);
Parcelable: Bundle.putParcelable(Key, 实现了Parcelable的对象);
2)内存使用性能上:

Parcelable > Serializable
由于Serializable在序列化时会产生大量的临时变量,从而引起频繁的GC。
3)数据存储在磁盘上的情况:

Serialzable > Parcelable
Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下。尽管Serializable效率低点,但此时还是建议使用Serializable
支持的成员数据类型:
1) 普通数据类型【如:int、double等以及他们的封装类如Integer、Float等】以及他们的数组类型【如:int[]、double[]等】注意:不支持:List[Integer]类型等List类型
2) String/CharSequence及其数组String[]、ArrayList[String]
3) Bundle 和 List
4) Serializable 注意:不支持List
5) Binder 和 List ,IBinder 和 List
6) Parcelable实现类
重温:Intent/Bundle传递支持的数据类型有:

基本数据类型和它的数组类型
String/CharSequence和它的数组类型
Serializable
Parcelable

而至于List<实体类> xxx如何读写,见下面代码片段,Cast和Avatars类都实现了Parcelable类,略过。

package com.helin.rxsample.enity;import android.annotation.SuppressLint;import android.os.Parcel;import android.os.Parcelable;import java.util.ArrayList;import java.util.List;/** * Created by liukun on 16/3/5. */@SuppressLint("ParcelCreator")public class Subject implements Parcelable{    private String id;    private String alt;    private String year;    private String title;    private String original_title;    private List<String> genres;    private List<Cast> casts = new ArrayList<>();    private List<Cast> directors = new ArrayList<>();    private Avatars images;    @Override    public String toString() {        return "Subject.id=" + id                + " Subject.title=" + title                + " Subject.year=" + year                + " Subject.originalTitle=" + original_title + casts.toString() + directors.toString() + " | ";    }    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public String getAlt() {        return alt;    }    public void setAlt(String alt) {        this.alt = alt;    }    public String getYear() {        return year;    }    public void setYear(String year) {        this.year = year;    }    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getOriginal_title() {        return original_title;    }    public void setOriginal_title(String original_title) {        this.original_title = original_title;    }    public List<String> getGenres() {        return genres;    }    public void setGenres(List<String> genres) {        this.genres = genres;    }    public List<Cast> getCasts() {        return casts;    }    public void setCasts(List<Cast> casts) {        this.casts = casts;    }    public List<Cast> getDirectors() {        return directors;    }    public void setDirectors(List<Cast> directors) {        this.directors = directors;    }    public Avatars getImages() {        return images;    }    public void setImages(Avatars images) {        this.images = images;    }    protected Subject(Parcel in){         id = in.readString();        alt = in.readString();        year = in.readString();        title = in.readString();        original_title = in.readString();        genres = in.createStringArrayList();//        in.readList(casts,casts.getClass().getClassLoader());//        in.readList(directors,directors.getClass().getClassLoader());        **in.readTypedList(casts,Cast.CREATOR);        in.readTypedList(directors,Cast.CREATOR);**        images = in.readParcelable(images.getClass().getClassLoader());    }    @Override    public int describeContents() {        return 0;    }    /* private String id;    private String alt;    private String year;    private String title;    private String original_title;    private List<String> genres;    private List<Cast> casts;    private List<Cast> directors;    private Avatars images;*/    @Override    public void writeToParcel(Parcel dest, int flags) {        dest.writeString(this.id);        dest.writeString(this.alt);        dest.writeString(this.year);        dest.writeString(this.title);        dest.writeString(this.original_title);        dest.writeStringList(this.genres);//        dest.writeList(this.casts);//        dest.writeList(this.directors);        **dest.writeTypedList(casts);        dest.writeTypedList(directors);**        dest.writeParcelable(this.images,flags);    }    public static final Parcelable.Creator<Subject> CREATOR = new Parcelable.Creator<Subject>(){        @Override        public Subject createFromParcel(Parcel source) {            return new Subject(source);        }        @Override        public Subject[] newArray(int size) {            return new Subject[size];        }    };}
0 0
原创粉丝点击