如何序列化自定义对象列表并在Android中的RestoreInstanceState中恢复它?

时间:2014-03-20 15:17:14

标签: java android serializable

大家好我有一个自定义对象MasterWithValue(一个带有值的对象和我制作的Detail对象列表),它扩展了一个类Master(它只有一个name属性)。

这是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);
}

}

在onSaveInstanceState()方法中,我有这段代码:

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

MasterAndDetailStatistics是List。

现在在onRestoreInstanceState中我尝试了这段代码:

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

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

}

我收到类型安全警告:类型安全:从Serializable到List的未选中强制转换如何检查?我读到我应该实现Parcable接口,但我是android的新手,我不知道如何做到这一点。我该怎么办?

1 个答案:

答案 0 :(得分:3)

有很多关于如何实现parcelable的例子。 Here is the canonical one. Here is an answer from SO.

基本上,您实现了一个方法public void writeToParcel(Parcel out, int flags),您可以将所有要保留的字段写入out值。

然后实现使用parcelable类参数化的Parcelable.Creator的匿名实现。此匿名实现必须命名为CREATOR

最后,创建一个这样的私有构造函数:private MyParcelable(Parcel in)。您在out中将您在in中写下的所有值读回到对象的字段中。

然后你需要制作细节也可以!哦,快乐!

它需要看起来像这样:

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