我使用的是具有LatLong对象的mapsforge库,用于在地图上存储点。不幸的是,它只实现了Serializable接口,而不是Parcelable。
public class LatLong implements Comparable<LatLong>, Serializable
在我的应用程序中,我有对象(我们称之为结果),其中包含LatLong点列表:
List<LatLong> points;
我的类Result实现了Parcelable。
我的问题是如何写入Parcel List,其中LatLong是Serializable但不是Parcelable? writeSerializable,writeTypedList不起作用。
答案 0 :(得分:2)
在writeToParcel()
方法中,使用以下代码:
dest.writeInt(latLongList.size());
for (LatLong latLong : latLongList) {
dest.writeDouble(latLong.latitude);
dest.writeDouble(latLong.longitude);
}
在从CREATOR.createFromParcel(Parcel source)
调用的私有对象构造函数中,使用以下代码:
latLongList = new ArrayList<LatLong>();
int size = source.readInt();
for (int i = 0; i < size; i++) {
double lat = source.readDouble();
double lon = source.readDouble();
latLongList.add(new LatLong(lat, lon));
}
我的基础是我在mapsforge.org找到的JavaDocs