使用onSaveInstanceState()存储和检索自定义ArrayList

时间:2014-05-28 10:44:16

标签: java android google-maps arraylist

在我的Android应用程序上,我有一张地图,我在上面绘制了一些标记。所有标记都存储在List<MapPoint> MapPointsList = new ArrayList<MapPoint>()变量中。 这是MapPoint类的代码:

    public class MapPoint {
    private String name;
    private Double latitude;
    private Double longitude;
    private String status;
    private String kind;
    private String realname;
    private Boolean selected;

    MapPoint (String nam, Double lat, Double lon, String sta, String kin) {
        name = nam;
        latitude = lat;
        longitude = lon;
        status = sta;
        kind = kin;

        realname = "PF" + name.substring(0, 2);
        selected = false;
    }
    }

我需要保存标记,以便在活动恢复时我可以在地图上重新添加它(例如用户打开然后关闭第二个活动),所以我创建了一个像这样的parcelable类:

    public class MapPointsParcelable implements Parcelable {
    String name;
    Double latitude;
    Double longitude;
    String status;
    String kind;
    String realname;
    Boolean selected;

    public MapPointsParcelable(String name, Double latitude, Double longitude, String status, String kind, String realname, Boolean selected) {
    this.name = name;
    this.latitude = latitude;
    this.longitude = longitude;
    this.status = status;
    this.kind = kind;
    this.realname = realname;
    this.selected = selected;
    }

    private MapPointsParcelable(Parcel in) {
    name = in.readString();
    latitude = in.readDouble();
    longitude = in.readDouble();
    status = in.readString();
    kind = in.readString();
    realname = in.readString();
    selected = in.readInt() == 0;
    }

    public int describeContents() {
    return 0;
    }

    @Override
    public String toString() {
    return "";//number + ": " + color;
    }

    public void writeToParcel(Parcel out, int flags) {
    out.writeString(name);
    out.writeDouble(latitude);
    out.writeDouble(longitude);
    out.writeString(status);
    out.writeString(kind);
    out.writeString(realname);
    out.writeInt(selected ? 0 : 1);
    }

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

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

然后我使用此代码通过onSaveInstanceState保存数据:

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);

    savedInstanceState.putDouble("mCurrentMapCenterLatitude", mCurrentMapCenterLatitude);
    savedInstanceState.putDouble("mCurrentMapCenterLongitude", mCurrentMapCenterLongitude);
    savedInstanceState.putFloat("mCurrentMapZoom", mCurrentMapZoom);

    ArrayList<MapPointsParcelable> list;
    list = new ArrayList<MapPointsParcelable>();

    for(int n = 0; n<MapPointsList.size()-1; n++) {
        list.add(new MapPointsParcelable(MapPointsList.get(n).name, MapPointsList.get(n).latitude, MapPointsList.get(n).longitude, MapPointsList.get(n).status, MapPointsList.get(n).kind, MapPointsList.get(n).realname, MapPointsList.get(n).selected));
    }

    savedInstanceState.putParcelableArrayList("MapPointsList", list);
    }

这是为了恢复它们:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    if (savedInstanceState != null) {
        mCurrentMapCenterLatitude = savedInstanceState.getDouble("mCurrentMapCenterLatitude");
        mCurrentMapCenterLongitude = savedInstanceState.getDouble("mCurrentMapCenterLongitude");
        mCurrentMapZoom = savedInstanceState.getFloat("mCurrentMapZoom");

        MapPointsList.clear();
        MapPolylinesList.clear();

        ArrayList<MapPointsParcelable> list;
        list = savedInstanceState.getParcelableArrayList("MapPointsList");

        for(int n = 0; n<list.size()-1; n++) {
            MapPoint mapPoint = new MapPoint(list.get(n).name, list.get(n).latitude, list.get(n).longitude, list.get(n).status, list.get(n).kind);
            mapPoint.selected = list.get(n).selected;
            MapPointsList.add(mapPoint);
        }

        mapRestore = true;
    }
    }

我的问题是这样:所有数据都已恢复,但没有标记为MapPointsList,因此我无法在地图上添加标记。

我也尝试使用onRestoreInstanceState,但结果相同。

我希望有人能帮我找到问题。

1 个答案:

答案 0 :(得分:0)

如果你只有一个标记,那么你肯定不会保存任何东西。

for(int n = 0; n<MapPointsList.size()-1; n++) {

这样您就可以忽略最后一项。不需要从阵列的大小中减去1。如果列表中只有一个项目,则条件如下所示:0 < 0,因此它将忽略您拥有的唯一元素。

迭代List的首选(自动防故障)方法是:

Iterator<MapPoint> iter = MapPointsList.iterator();

while(iter.hasNext()){
   MapPoint next = iter.next();
   // do your logic
}