首页 > 代码库 > Android 使用 Parcelable 定义可传递数据的Model
Android 使用 Parcelable 定义可传递数据的Model
Parcelable数据
public class HotelRoom implements Parcelable { public HotelRoom () { this.adultOrderNumber = 1; this.roomNumber = 1; this.kidOrderNumber = 0; } /** 成年人订单数量*/ private int adultOrderNumber; /** 房间数量*/ private int roomNumber; /** 儿童订单数量*/ private int kidOrderNumber; /**儿童年龄*/ private int[] childAges = null; public int[] getChildAges() { return childAges; } public void setChildAges(int[] childAges) { this.childAges = childAges; } public int getAdultOrderNumber() { return adultOrderNumber; } public void setAdultOrderNumber(int adultOrderNumber) { this.adultOrderNumber = adultOrderNumber; } public int getRoomNumber() { return roomNumber; } public void setRoomNumber(int roomNumber) { this.roomNumber = roomNumber; } public int getKidOrderNumber() { return kidOrderNumber; } public void setKidOrderNumber(int kidOrderNumber) { this.kidOrderNumber = kidOrderNumber; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(this.adultOrderNumber); dest.writeInt(this.kidOrderNumber); dest.writeInt(this.roomNumber); if(childAges==null) { childAges = new int[0]; //注意,这里必须进行处理,否则会出现空指针异常 } dest.writeIntArray(childAges); } public static final Parcelable.Creator<HotelRoom > CREATOR = new Parcelable.Creator<HotelRoom >() { public HotelRoom createFromParcel(Parcel source) { HotelRoom roomOrder = new HotelRoom (); roomOrder.adultOrderNumber = source.readInt(); roomOrder.kidOrderNumber = source.readInt(); roomOrder.roomNumber = source.readInt(); roomOrder.childAges = new int[roomOrder.kidOrderNumber]; source.readIntArray(roomOrder.childAges); return roomOrder; } public HotelRoom [] newArray(int size) { return new HotelRoom [size]; } }; }
Android 使用 Parcelable 定义可传递数据的Model
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。