1

Hi everyone I have a custom object MasterWithValue (an object with a value and a list of Detail objects I have made) which extends a class Master (which has only a name property).

This is the class MasterWithValue:

public class MasterWithValue extends Master {

private String value;
private List<Detail> detailList;

public MasterWithValue(String masterName, String masterValue) {
    super(masterName);
    this.value = masterValue;
    this.detailList = new ArrayList<Detail>();
}

@Override 
public int getViewType() {
    return super.getViewType();
}

@Override
public View getView(LayoutInflater inflater, View convertView) {
    View view;
    if (convertView == null) {
        view = inflater.inflate(R.layout.statistics_rowlist_master, null);
    }
    else {
        view = convertView;
    }

    TextView MasterEntryName = (TextView) view.findViewById(R.id.statistics_master_name);
    TextView MasterEntryValue = (TextView) view.findViewById(R.id.statistics_master_value);

    MasterEntryName.setText(super.name);
    MasterEntryValue.setText(this.value);

    return view;
}

public String getMasterValue() {
    return value;
}

public List<Detail> getDetailList() {
    return this.detailList;
}

public void addDetailToMaster(Detail detail) {
    this.detailList.add(detail);
}

}

In the onSaveInstanceState() method I have this code:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putSerializable("master_detail_list", (ArrayList<MasterWithValue>)   
      MasterAndDetailStatistics);
}

Where MasterAndDetailStatistics is a List.

Now in onRestoreInstanceState I have tried this code:

@Override
protected void onRestoreInstanceState(Bundle savedState) {
    super.onRestoreInstanceState(savedState);

    MasterAndDetailStatistics = (List<MasterWithValue>) savedState.getSerializable("master_detail_list");

}

By I get a type-safety warning: Type safety: Unchecked cast from Serializable to List How can I check it? I read that I should implement the Parcable interface, but I am new to android and I have no idea on how to do that. What should I do?

tonix
  • 6,671
  • 13
  • 75
  • 136

1 Answers1

3

There are many examples of how to implement parcelable. Here is the canonical one. Here is an answer from SO.

Basically, you implement a method public void writeToParcel(Parcel out, int flags) where you write all the fields you want persisted into the out value.

Then implement an anonymous implementation of a Parcelable.Creator parameterized with your parcelable class. This anonymous implementation must be named CREATOR.

Finally, make a private constructor like this: private MyParcelable(Parcel in). Where you read all the values you wrote in the out back into the fields of your object in the in.

Then you need to make Detail parcelable too! Oh joy!

It needs to look like this:

public class MasterWithDetails implements Parcelable {
    private String value;
    private List<Detail> detailList;

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel out, int flags) {
        out.writeString(value);
        out.writeString(getName());
        out.writeTypedList(detailList); //don't forget to make Detail parcelable too!
    }

    public static final Parcelable.Creator<MasterWithDetails> CREATOR
            = new Parcelable.Creator<MasterWithDetails>() {
        public MasterWithDetails createFromParcel(Parcel in) {
            return new MasterWithDetails(in);
        }

        public MasterWithDetails[] newArray(int size) {
            return new MasterWithDetails[size];
        }
    };

    private MasterWithDetails(Parcel in) {
        value = in.readString();
        setName(in.readString());
        detailList = in.createTypedArrayList(Detail.CREATOR);
    }
}
Community
  • 1
  • 1
CorayThan
  • 17,174
  • 28
  • 113
  • 161
  • Thanks for the response. If a class MasterWithValue extends a Master class, they should implement the Parcelable interface together? I mean I implement the Parcelable interface in the Master class and then in the MasterWithValue I override those methods in order to save also the value of the MasterWithValue object as MasterWithValue has a value field that Master class doesn't. Am I right? – tonix Mar 21 '14 at 22:03
  • I also get this when I try to call the writeTypedArrayList(): The method writeTypedArrayList(List) is undefined for the type Parcel. Maybe you were intending to use writeTypedList(detailList)? – tonix Mar 21 '14 at 22:32
  • 1
    Yep, I probably was in a hurry and meant `writeTypedList`. For the descendant class you would need to override the `writeToParcel` method (although with a call to `super.writeToParcel` you wouldn't need to duplicate any logic). The constructor is private, so it can't be overriden or called and must be duplicated. The CREATOR field is public and static, so it should not be changed. Honestly, parcelable looks pretty ugly when you are using it with inheritance. Of course, I think it looks ugly in the first place. (I usually prefer to just serialize my classes as Strings (with JSON) or bytes.) – CorayThan Mar 22 '14 at 01:56
  • Thanks it works! Thank you for your explanation, you were very clear and simple! – tonix Mar 22 '14 at 08:04