Android筆記之Intent傳遞自訂對象

來源:互聯網
上載者:User

標籤:des   android   c   style   class   blog   

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;            private List<String> v_description;    /**     * 這一步read的順序要和writeToParcel的順序保持一致     * @param parcel     */    private ViewPoint(Parcel parcel) {        // TODO Auto-generated constructor stub        v_name=parcel.readString();        v_description=parcel.readArrayList(List.class.getClassLoader());        v_picture=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);        dest.writeList(v_picture);    }}

 

Done

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.