您现在的位置是:首页 > 文章详情

Android Parcelable数据序列化详解

日期:2018-08-20点击:565

什么是什么是Parcelable

Parcelable是Android sdk提供的用实现于数据序列化的一个接口,不同于Java中的基于磁盘或者网络的Serializable,Parcelable是基于内存的,由于内存的读写速度高于磁盘,因此在Android中跨进程对象传递一般使用Parcelable。

如何使用Parcelable

要想使用Parcelable并不容易,需要编写很多代码,如下:

package com.itfitness.androidparcelabletest;

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

public class BookBean implements Parcelable {
    private int bookId;
    private String bookName;
    private String bookAuthor;
    private String bookPrice;

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.bookId);
        dest.writeString(this.bookName);
        dest.writeString(this.bookAuthor);
        dest.writeString(this.bookPrice);
    }

    public BookBean() {
    }

    protected BookBean(Parcel in) {
        this.bookId = in.readInt();
        this.bookName = in.readString();
        this.bookAuthor = in.readString();
        this.bookPrice = in.readString();
    }

    public static final Parcelable.Creator<BookBean> CREATOR = new Parcelable.Creator<BookBean>() {
        @Override
        public BookBean createFromParcel(Parcel source) {
            return new BookBean(source);
        }

        @Override
        public BookBean[] newArray(int size) {
            return new BookBean[size];
        }
    };
}

Parcelable虽然好用但并不好使用,每次使用需要编写这么多代码显然很麻烦,但是因为我们使用的是Android Studio啊,Android Studio中有一个专门用来生成Parcelable代码的插件:Android Parcelable code generator,如下图所示。

img_f66391de69990dcb5519cc483e33d0f2.png

安装完成后重启Android Studio然后编写一个BookBean类,按Alt+Insert会弹出一个窗口。

img_0b43c4c6af1665cbc2f6bcbd8c61a76e.png

选择Parcelable选项,Android Studio会自动生成代码,实现Parcelable接口。

package com.itfitness.androidparcelabletest;

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

public class BookBean implements Parcelable {
    private int bookId;
    private String bookName;
    private String bookAuthor;
    private String bookPrice;

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.bookId);
        dest.writeString(this.bookName);
        dest.writeString(this.bookAuthor);
        dest.writeString(this.bookPrice);
    }

    public BookBean() {
    }

    protected BookBean(Parcel in) {
        this.bookId = in.readInt();
        this.bookName = in.readString();
        this.bookAuthor = in.readString();
        this.bookPrice = in.readString();
    }

    public static final Parcelable.Creator<BookBean> CREATOR = new Parcelable.Creator<BookBean>() {
        @Override
        public BookBean createFromParcel(Parcel source) {
            return new BookBean(source);
        }

        @Override
        public BookBean[] newArray(int size) {
            return new BookBean[size];
        }
    };
}

Activity中的传值

MainActivity
 final BookBean bookBean=new BookBean();
        bookBean.setBookId(1);
        bookBean.setBookAuthor("阿萨德");
        bookBean.setBookName("水浒传");
        bookBean.setBookPrice("¥24");
        findViewById(R.id.bt_test).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, TestActivity.class);
                intent.putExtra("testData",bookBean);
                startActivity(intent);
            }
        });
TestActicity
 BookBean bookBean = getIntent().getParcelableExtra("testData");
        Log.e("测试",bookBean.getBookId()+"==="+bookBean.getBookAuthor()+"==="+bookBean.getBookName()+"==="+bookBean.getBookPrice());
img_8aec547a3205fdd27853eb2fad905007.png
原文链接:https://yq.aliyun.com/articles/665556
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章