android中對象Parcelable化

來源:互聯網
上載者:User

  對象序列化找個東西我不知道怎麼用。個人認為許多需要傳遞大對象的情況都可以避免掉。比如把多個acitivyt的內容寫到一個activity中(找個好處還可以同時可以finish掉許多activity哦)。但是作為一個知識,還是寫寫了,也因為我n久沒有更新部落格了。

  介紹的話就不準備寫了,還是代碼簡單:

  

public class ImageEntity implements Parcelable{    String name;    String path;    String path1; // first path, default path . replace with selector    String path2; // second path, default path    int id;    boolean isSelected;        public ImageEntity(String path) {        this.name = path;        this.path = this.path1 = this.path2 = path;        this.isSelected = false;    }    public ImageEntity(String path1, String path2){        this.name = path1;        this.path1 = path1;        this.path2 = path2;    }    public ImageEntity(String name, String path1, String path2){        this.name = name;        this.path1 = path1;        this.path2 = path2;    }    public ImageEntity(String name, int id){        this.name = name;        this.path = "";        this.id = id;        this.isSelected = false;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPath() {        return path;    }    public void setPath(String path) {        this.path = path;    }    public String getPath1() {        return path1;    }    public void setPath1(String path1) {        this.path1 = path1;    }    public String getPath2() {        return path2;    }    public void setPath2(String path2) {        this.path2 = path2;    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public boolean isSelected() {        return isSelected;    }    public void setSelected(boolean isSelected) {        this.isSelected = isSelected;    }        @Override    public int describeContents() {        // TODO Auto-generated method stub        return 0;    }    @Override    public void writeToParcel(Parcel dest, int flags) {        dest.writeString(name);        dest.writeString(path);        dest.writeInt(id);        dest.writeByte((byte) (isSelected ? 1 : 0));     //if isSelected == true, byte == 1    }    public static final Parcelable.Creator<ImageEntity> CREATOR = new Parcelable.Creator<ImageEntity>(){        @Override        public ImageEntity createFromParcel(Parcel source) {                        return new ImageEntity(source);        }        @Override        public ImageEntity[] newArray(int size) {            // TODO Auto-generated method stub            return new ImageEntity[size];        }            };    private ImageEntity(Parcel parcel){        name = parcel.readString();        path = parcel.readString();        id = parcel.readInt();        isSelected = parcel.readByte() == 1;     //isSelected == true if byte == 1    }}

  還又一個沒完:

/** * Purpose: object that translate between activity.  it may contains image-objects and may be more * Author: Slider Xiao * Created Time: Mar 12, 2013 5:04:00 PM  * Update By: Slider Xiao, Mar 12, 2013 5:04:00 PM */public class ImagesIntent implements Parcelable{    private ArrayList<ImageEntity> imageEntities;    public ImagesIntent(){    }    public ArrayList<ImageEntity> getImageEntities() {        return imageEntities;    }    public void setImageEntities(ArrayList<ImageEntity> imageEntities) {        this.imageEntities = imageEntities;    }    @Override    public int describeContents() {        // TODO Auto-generated method stub        return 0;    }    @Override    public void writeToParcel(Parcel dest, int flags) {        // TODO Auto-generated method stub        dest.writeTypedList(imageEntities);    }        public static final Parcelable.Creator<ImagesIntent> CREATOR = new Parcelable.Creator<ImagesIntent>(){        @Override        public ImagesIntent createFromParcel(Parcel source) {            // TODO Auto-generated method stub            return new ImagesIntent(source);        }        @Override        public ImagesIntent[] newArray(int size) {            // TODO Auto-generated method stub            return new ImagesIntent[size];        }            };    private ImagesIntent(Parcel parcel){        imageEntities = new ArrayList<ImageEntity>();        parcel.readTypedList(imageEntities, ImageEntity.CREATOR);    }}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.