安卓中的序列化(Parcelable/Serializable)

1、对象序列化
1.1、永久性保存对象,保存对象的字节序列到本地文件中;
1.2、用过序列化对象在网络中传递对象;
1.3、通过序列化对象在进程间传递对象。

2、实现Serializable接口(java)
Serializable的作用是将数据对象存入字节流当中,在需要时重新生成对象,
主要应用是利用外部存储设备保存对象状态,以及通过网络传输对象等。
///Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC。

3、Android中的新的序列化机制–Parcelable接口
Parcel的序列化和反序列化的读写全是在内存中进行
///在常使用Parcel类的场景就是在Activity间传递数据。在Activity间使用Intent传递数据的时候,可以通过Parcelable机制传递复杂的对象。
///Intent传值的案例:

package com.example.intent_object;

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

public class User implements Parcelable {
    public String name;
    public int age;

    // 必须要创建一个名叫CREATOR的常量。
    public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
        @Override
        public User createFromParcel(Parcel source) {
            return new User(source);
        }
        //重写createFromParcel方法,创建并返回一个获得了数据的user对象
        @Override
        public User[] newArray(int size) {
            return new User[size];
        }
    };
    @Override
    public String toString() {
        return name + ":" + age;
    }

    // 无参数构造器方法,供外界创建类的实例时调用
    public User() {
    }
    // 带参构造器方法私用化,本构造器仅供类的方法createFromParcel调用
    private User(Parcel source) {
        name = source.readString();
        age = source.readInt();
    }

    @Override
    public int describeContents() {
        return 0;
    }
    // 将对象中的属性保存至目标对象dest中
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
    }
&nbsp; //省略getter/setter }

///其他类型:
(1)/boolean
writeToParcel:
dest.writeInt(isHasSign ? 1 : 0);

(2)/CREATOR
profile.setHasSign(source.readInt() == 1 ? true : false);

(3)/List
dest.writeList(photos);
profile.setPhotos(source.readArrayList(NearByPeopleProfile.class
.getClassLoader()));

(4)、实例:

package com.test.parcelable;

import java.util.List;
import java.util.Map;

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

/**
 * 照片内容数据的实�?
 * 
 * @author rendongwei
 * 
 */
public class PhotoDetailResult implements Parcelable {
    /**
     * 照片的编�?
     */
    private int image;
    /**
     * 照片的上传时�?
     */
    private String time;
    /**
     * 照片的描�?
     */
    private String description;
    /**
     * 照片的评论数�?
     */
    private int comment_count;
    /**
     * 照片的赞数量
     */
    private int like_count;
    /**
     * 照片的评�?
     */
    private List<Map<String, Object>> comments;
    private boolean isbool;

    private List list;

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public boolean isIsbool() {
        return isbool;
    }

    public void setIsbool(boolean isbool) {
        this.isbool = isbool;
    }
    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getComment_count() {
        return comment_count;
    }

    public void setComment_count(int comment_count) {
        this.comment_count = comment_count;
    }

    public int getLike_count() {
        return like_count;
    }

    public void setLike_count(int like_count) {
        this.like_count = like_count;
    }

    public List<Map<String, Object>> getComments() {
        return comments;
    }

    public void setComments(List<Map<String, Object>> comments) {
        this.comments = comments;
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(image);
        dest.writeInt(comment_count);
        dest.writeInt(like_count);
        dest.writeString(time);
        dest.writeString(description);
        dest.writeList(comments);
        dest.writeInt(isbool ? 1 : 0);
        dest.writeList(list); 
    }

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

        @SuppressWarnings("unchecked")
        public PhotoDetailResult createFromParcel(Parcel source) {
            PhotoDetailResult result = new PhotoDetailResult();
            result.setImage(source.readInt());
            result.setComment_count(source.readInt());
            result.setLike_count(source.readInt());
            result.setTime(source.readString());
            result.setDescription(source.readString());
            result.setComments(source.readArrayList(PhotoDetailResult.class
                    .getClassLoader()));
            result.setIsbool(source.readInt() == 1 ? true : false);
            result.setList(source.readArrayList(PhotoDetailResult.class 
                     .getClassLoader()));
            return result;
        }

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

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值