Android中包含List成员变量的Parcel以及Parcel嵌套写法示例

 

这个Scean类实现了Parcelable接口,同时其内部的成员变量List<SubScean> subSceanList 中的SubScean类也实现了Parcelable接口。

public class Scean implements Parcelable {
    public String name;
    public String voiceName;
    public String picName;
    public String introduction;
    public String folderName;
    
    public Double rightTopLon;
    public Double rightTopLat;
    public Double leftBottomLon;
    public Double leftBottomLat;
    
    public int picPosition;
    public int picLength;
    public int introPosition;
    public int introLength;
    
    public String cityName;
    public int totalLength;

    private List<SubScean> subSceanList = new ArrayList<SubScean>();

    public Scean() {
        // do nothing;
    }

    private Scean(Parcel in) {
        name = in.readString();
        voiceName = in.readString();
        picName = in.readString();
        introduction = in.readString();
        folderName = in.readString();

        rightTopLon = in.readDouble();
        rightTopLat = in.readDouble();
        leftBottomLon = in.readDouble();
        leftBottomLat = in.readDouble();
        
        picPosition = in.readInt();
        picLength = in.readInt();
        introPosition = in.readInt();
        introLength = in.readInt();
        cityName = in.readString();
        totalLength = in.readInt();
        
        in.readTypedList(subSceanList, SubScean.CREATOR);// return void, it pass
                                                            // List to the first
                                                            // arg
    }

    public void addSubScean(SubScean subScean) {
        subSceanList.add(subScean);
    }

    public SubScean getSubScean(int position) {
        return subSceanList.get(position);
    }

    public int getLength() {
        return subSceanList.size();
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.name);
        dest.writeString(this.voiceName);
        dest.writeString(this.picName);
        dest.writeString(this.introduction);
        dest.writeString(this.folderName);

        dest.writeDouble(this.rightTopLon);
        dest.writeDouble(this.rightTopLat);
        dest.writeDouble(this.leftBottomLon);
        dest.writeDouble(this.leftBottomLat);
        
        dest.writeInt(picPosition);
        dest.writeInt(picLength);
        dest.writeInt(introPosition);
        dest.writeInt(introLength);
        dest.writeString(cityName);
        dest.writeInt(totalLength);
        
        dest.writeTypedList(subSceanList);
    }

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

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

        @Override
        public Scean createFromParcel(Parcel source) {
            return new Scean(source);
        }
    };
}

下面是SubScean的代码

public class SubScean implements Parcelable {
    public String subScenName;
    public String voiceName;
    public String picName;
    public String folderName;
    public String voiceSuffix;
    public Double longitude;
    public Double latitude;
    
    public int picPosition;
    public int picLength;
    
    public String cityName;
    public String scenicName;
    
    public int introPosition;
    public int introLength;

    public SubScean() {
    }

    private SubScean(Parcel in) {
        this.subScenName = in.readString();
        this.voiceName = in.readString();
        this.picName = in.readString();
        this.folderName = in.readString();
        
        this.longitude = in.readDouble();
        this.latitude = in.readDouble();
        
        picPosition = in.readInt();
        picLength = in.readInt();
        
        cityName = in.readString();
        scenicName = in.readString();
        
        introPosition = in.readInt();
        introLength = in.readInt();
    }


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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(subScenName);
        dest.writeString(voiceName);
        dest.writeString(picName);
        dest.writeString(folderName);
        dest.writeDouble(longitude);
        dest.writeDouble(latitude);
        
        dest.writeInt(picPosition);
        dest.writeInt(picLength);
        
        dest.writeString(cityName);
        dest.writeString(scenicName);
        
        dest.writeInt(introPosition);
        dest.writeInt(introLength);
    }

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

        @Override
        public SubScean createFromParcel(Parcel source) {
            return new SubScean(source);
        }

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

    };
}

 

posted @ 2017-09-07 16:33  BlogCommunicator  阅读(1686)  评论(0编辑  收藏  举报