Android笔记之Intent传递自定义对象

1、

import java.util.List;
import android.os.Parcel;
import android.os.Parcelable;

/**
 * 1)writeToParcel 方法。该方法将类的数据写入外部提供的Parcel中。
 * 2)describeContents方法。直接返回0就可以。
 * 3)静态的Parcelable.Creator<T>接口,本接口有两个方法:
 * createFromParcel(Parcel in) 实现从in中创建出类的实例的功能。
 * newArray(int size) 创建一个类型为T,长度为size的数组, returnnew T[size];即可。本方法是供外部类反序列化本类数组使用。
 * 
 * @author JL
 */
public class ViewPoint implements Parcelable {
    
    private String v_name;
    
    private List<String> v_picture;
     /**
     * 这一步read的顺序要和writeToParcel的顺序保持一致
     * @param parcel
     */
    private ViewPoint(Parcel parcel) {
        // TODO Auto-generated constructor stub
        v_name=parcel.readString();
        v_description=parcel.readArrayList(List.class.getClassLoader());
//List<String> :parcel.readArrayList(List.class.getClassLoader())
        //Map<String, String> :parcel.readHashMap(Map.class.getClassLoader())
        //List<School> :parcel.readArrayList(School.class.getClassLoader())
        //School :(School) parcel.readParcelable(School.class.getClassLoader())
    }
    public ViewPoint() {
        // TODO Auto-generated constructor stub
    }
    public static final Parcelable.Creator<ViewPoint> CREATOR=new Parcelable.Creator<ViewPoint>() {

        @Override
        public ViewPoint createFromParcel(Parcel source) {
            // TODO Auto-generated method stub
            return new ViewPoint(source);
        }

        @Override
        public ViewPoint[] newArray(int size) {
            // TODO Auto-generated method stub
            return new ViewPoint[size];
        }
    };

    
    public String getV_name() {
        return v_name;
    }
    public void setV_name(String v_name) {
        this.v_name = v_name;
    }
    public List<String> getV_picture() {
        return v_picture;
    }
    public void setV_picture(List<String> v_picture) {
        this.v_picture = v_picture;
    }
    public List<String> getV_description() {
        return v_description;
    }
    public void setV_description(List<String> v_description) {
        this.v_description = v_description;
    }
    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub
        //向Parcel对象写入要序列化的数据
        //List一律写成 :writeList
        //School :writeParcelable(schoolInfo, arg1),且School本身是Parcelable对象
        dest.writeString(v_name);
        dest.writeList(v_description);
    }
}

byte, double, float, int, long和上面的String类似,套用即可。以下为特殊的几种类型:

Date

dest.writeLong(startDate.getTime());

startDate=new Date(source.readLong());

布尔类型

dest.writeByte((byte) (isLike ? 1 : 0));
isLike = source.readByte() != 0;  

 parcel类型(School是parcel类型)

dest.writeParcelable(school, PARCELABLE_WRITE_RETURN_VALUE);
school=(School) parcel.readParcelable(School.class.getClassLoader())

List<E>s所有类型的List的同统一写法

Done

原文地址:https://www.cnblogs.com/xingyyy/p/3761838.html