zoukankan      html  css  js  c++  java
  • Parcelable和Parcel

    Parcelable   传递对象时候用

    Android序列化对象主要有两种方法,实现Serializable接口、或者实现Parcelable接口。实现Serializable接口是JavaSE本身就支持的,而Parcelable是Android特有的功能,效率比实现Serializable接口高,而且还可以用在进程间通信(IPC)中

    只有实现了Parcelable接口的类才能被放入Parcel中。

    Parcelable实现要点:需要实现三个东西

    1)writeToParcel 方法。该方法将类的数据写入外部提供的Parcel中.声明如下:

    writeToParcel (Parcel dest, int flags) 具体参数含义见javadoc

    2)describeContents方法。没搞懂有什么用,反正直接返回0也可以

    3)静态的Parcelable.Creator接口,本接口有两个方法:

    createFromParcel(Parcel in) 实现从in中创建出类的实例的功能

    newArray(int size) 创建一个类型为T,长度为size的数组,仅一句话(return new T[size])即可。估计本方法是供外部类反序列化本类数组使用。

     

    Intent中的运用如下:

    bundle.putParcelable("key", passObject);
    ParcelableObject passObject = this.getIntent()
    				.getParcelableExtra("key");

     
    简单实例

    import android.os.Parcel;
    import android.os.Parcelable;

    public class FilmInfo implements Parcelable {

    private String filmName;
    private String director;
    private String starring;
    private String produce;
    private String summary;
    private String actionTime;
    private String imgUrl;

    public static final Parcelable.Creator<FilmInfo> CREATOR = new Creator<FilmInfo>() {

    @SuppressWarnings("unchecked")
    public FilmInfo createFromParcel(Parcel source) {
    FilmInfo filmInfo = new FilmInfo();
    filmInfo.filmName = source.readString();
    filmInfo.director = source.readString();
    filmInfo.starring = source.readString();
    filmInfo.produce = source.readString();
    filmInfo.summary = source.readString();
    filmInfo.actionTime = source.readString();
    filmInfo.imgUrl = source.readString();
    return filmInfo;
    }

    public FilmInfo[] newArray(int size) {
    return new FilmInfo[size];
    }

    };

    @Override
    public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
    // 按顺序写 否则数据错位
    dest.writeString(filmName);
    dest.writeString(director);
    dest.writeString(starring);
    dest.writeString(produce);
    dest.writeString(summary);
    dest.writeString(actionTime);
    dest.writeString(imgUrl);
    }

    public String getDirector() {
    return director;
    }

    public void setDirector(String director) {
    this.director = director;
    }

    public String getStarring() {
    return starring;
    }

    public void setStarring(String starring) {
    this.starring = starring;
    }

    public String getSummary() {
    return summary;
    }

    public void setSummary(String summary) {
    this.summary = summary;
    }

    public String getImgUrl() {
    return imgUrl;
    }

    public void setImgUrl(String imgUrl) {
    this.imgUrl = imgUrl;
    }

    public String getFilmName() {
    return filmName;
    }

    public void setFilmName(String filmName) {
    this.filmName = filmName;
    }

    public String getProduce() {
    return produce;
    }

    public void setProduce(String produce) {
    this.produce = produce;
    }

    public String getActionTime() {
    return actionTime;
    }

    public void setActionTime(String actionTime) {
    this.actionTime = actionTime;
    }

    }

    
    
  • 相关阅读:
    THINKPHP增删改查--(改)
    thinkphp中各字母代表的发放和具体实例
    PHP时间戳和日期互转换
    jquery方法大全
    Bootstrap里的文件分别表示什么?都有什么用?
    文本一处隐藏显示
    点击下拉,其余不动的jquery事件(转)
    JQuery中$.ajax()方法参数详解
    数据库联合查询输出数量
    如何使div居中
  • 原文地址:https://www.cnblogs.com/xingmeng/p/2569967.html
Copyright © 2011-2022 走看看