Android - 实现Parcelable Inner Class

时间:2016-03-10 17:17:00

标签: android inner-classes parcelable

我知道如何使用公共变量实现一个简单的Parcelable类,但下面的类稍微复杂一些。如果此类具有内部类和ListEntity,我如何实现Parcelable接口?我甚至不确定如何开始。任何有关如何执行此操作的详细信息将不胜感激。

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

import java.util.List;

public class ResponsePlaceSearch implements Parcelable {

    // *** Parcelable methods are shown below (Begin) ***

    protected ResponsePlaceSearch(Parcel in) {
    }

    public static final Creator<ResponsePlaceSearch> CREATOR = new Creator<ResponsePlaceSearch>() {
        @Override
        public ResponsePlaceSearch createFromParcel(Parcel in) {
            return new ResponsePlaceSearch(in);
        }

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

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {

    }

    // *** (End) ***


    private List<ListEntity> list;

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

    public List<ListEntity> getList() {
        return list;
    }

    public static class ListEntity {
        private int id;
        private String name;

        private CoordEntity coord;
        private int dt;

        private WindEntity wind;

        private SysEntity sys;

        public void setId(int id) {
            this.id = id;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setCoord(CoordEntity coord) {
            this.coord = coord;
        }

        public void setDt(int dt) {
            this.dt = dt;
        }

        public void setWind(WindEntity wind) {
            this.wind = wind;
        }

        public void setSys(SysEntity sys) {
            this.sys = sys;
        }

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public CoordEntity getCoord() {
            return coord;
        }

        public int getDt() {
            return dt;
        }

        public WindEntity getWind() {
            return wind;
        }

        public SysEntity getSys() {
            return sys;
        }

        public static class CoordEntity {
            private double lon;
            private double lat;

            public void setLon(double lon) {
                this.lon = lon;
            }

            public void setLat(double lat) {
                this.lat = lat;
            }

            public double getLon() {
                return lon;
            }

            public double getLat() {
                return lat;
            }
        }

        public static class WindEntity {
            private double speed;
            private double deg;

            public void setSpeed(double speed) {
                this.speed = speed;
            }

            public void setDeg(double deg) {
                this.deg = deg;
            }

            public double getSpeed() {
                return speed;
            }

            public double getDeg() {
                return deg;
            }
        }

        public static class SysEntity {
            private String country;

            public void setCountry(String country) {
                this.country = country;
            }

            public String getCountry() {
                return country;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:4)

您只需要为每个内部类(ListEntity,SysEntity,...)实现Parcelable接口,因此它们包含的所有类和类都实现了Parcelable。

然后使用

将类添加到宗地
public final void writeParcelable (Parcelable p, int parcelableFlags)

其中p是内部类的实例。

编辑:这是一个如何打包内部类的示例:

public class SampleParcelable implements Parcelable {
    public static class InnerClass implements Parcelable {
        private String mInnerString;
        private long mInnerLong;

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

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(mInnerString);
            dest.writeLong(mInnerLong);
        }

        public static final Creator<InnerClass> CREATOR = new Creator<InnerClass>() {
            public InnerClass createFromParcel(Parcel in) {
                return new InnerClass(in);
            }

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

        private InnerClass(Parcel in) {
            mInnerString = in.readString();
            mInnerLong = in.readLong();
        }
    }

    private String mString;
    private long mLong;
    private InnerClass mInnerClass;

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mString);
        dest.writeLong(mLong);
        dest.writeParcelable(mInnerClass, flags);
    }

    public static final Creator<SampleParcelable> CREATOR = new Creator<SampleParcelable>() {
        public SampleParcelable createFromParcel(Parcel in) {
            return new SampleParcelable(in);
        }

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

    private SampleParcelable(Parcel in) {
        mString = in.readString();
        mLong = in.readLong();
        mInnerClass = in.readParcelable(InnerClass.class.getClassLoader());
    }
}