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]; } }; }
时间: 2024-10-09 23:15:29