How to serialize a List of custom objects and recover it onRestoreInstanceState in Android?

StackOverflow https://stackoverflow.com/questions/22537389

  •  18-06-2023
  •  | 
  •  

سؤال

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?

هل كانت مفيدة؟

المحلول

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);
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top