0
我有一些困難時刻想弄清楚如何用Parcel對象寫數據,我是新來的使用這個類,我一直在做一些研究,但不明白如何在這裏是parcelable類用包裹對象寫數據
public class Item implements Parcelable {
private String iItemName;
private String iType;
private String iSerial;
private String iRetailer;
private String iLocation;
private String iValue;
private String iDescription;
public Item() {
super();
}
public Item (Parcel in) {
super();
this.iItemName = in.readString();
this.iType = in.readString();
this.iSerial = in.readString();
this.iRetailer = in.readString();
this.iLocation = in.readString();
this.iValue = in.readString();
this.iDescription = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(getItemName());
parcel.writeString(getType());
parcel.writeString(getSerial());
parcel.writeString(getRetailer());
parcel.writeString(getLocation());
parcel.writeString(getValue());
parcel.writeString(getDescription());
}
public static final Parcelable.Creator<Item> CREATOR = new Parcelable.Creator<Item>(){
@Override
public Item createFromParcel(Parcel source) {
Item item = new Item();
item.iItemName = source.readString();
return item;
}
public Item [] newArray(int size) {
return new Item[size];
}
};
public String getItemName() {
return iItemName;
}
public void setItemName(String iItemName) {
this.iItemName = iItemName;
}
我的問題是,如果我想創建一個新的項目對象我需要發送作爲參數地塊對象是嗎?所以我可以這樣做:
String name = getString("name");
// how to add name to the parcel object?
Parcel parcel;
Item i = new Item(parcel);
感謝無禮的粗魯的人, – Juanca