对象序列化找个东西我不知道怎么用。个人认为许多需要传递大对象的情况都可以避免掉。比如把多个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); } }