0

I am having some hard time trying to figure out how to write data with a Parcel object, I am new using this class and I've been doing some research but don't understand how to, here is the parcelable class

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;
}

my question is if I want to create a new Item object I need to send as a parameter a Parcel Object am I right? so I can do something like:

String name = getString("name");

// how to add name to the parcel object?

Parcel parcel;

Item i = new Item(parcel);
Jhonycage
  • 759
  • 3
  • 16
  • 36

1 Answers1

3

No you do not need to. Your object now has two constructors. You should create the object like normal and call its setter methods. You should avoid manually creating parcels and rather use writeValue() and readValue() methods.

// Create an item as usual
Item myItem = new Item();
myItem.setItemName("name");

// Add it to a parcel
Parcel parcel = Parcel.obtain();
parcel.writeValue(myItem);

// Read the item back from the parcel
parcel.setDataPosition(0);
Item newItem = (Item) parcel.readValue(Item.class.getClassLoader());

// When you are finished with the parcel
parcel.recycle();

Usually you would create an object from a Parcel when receiving it from an Intent. In which case you would do the following.

// Create intent
Intent intent = new Intent(this, MyActivity.class);
Bundle itemBundle = new Bundle();
itemBundle.putParcelable("item_extra", myItem);
intent.putExtras(itemBundle);
startActivity(intent);

// In the activities onCreate(Bundle savedInstanceState)
Item myItem = getIntent().getParcelableExtra("item_extra");
Breavyn
  • 2,232
  • 16
  • 29