Android - 可打包 | putParcelableArrayListExtra("ListItems", listItems);
Android - Parcelable | putParcelableArrayListExtra("ListItems", listItems);
我正在尝试将对象 MyItem 的 ArrayList 从一个 activity 传递到另一个。据我了解,我需要在 MyItem class 中实现 Parcable。这就是我到目前为止所做的:
public class MyItem implements ClusterItem, Parcelable {
private LatLng mPosition=null;
private String aString;
public MyItem(double lat, double lng, String aString) {
mPosition = new LatLng(lat, lng);
this.aString= aString;
}
@Override
public LatLng getPosition() {
return mPosition;
}
public String getString(){
return aString;
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
//dest.writeLngLat ?
dest.writeString(postID);
}
public static final Parcelable.Creator<MyItem> CREATOR
= new Parcelable.Creator<MyItem>() {
public MyItem createFromParcel(Parcel in) {
return new MyItem(in);
}
public MyItem[] newArray(int size) {
return new MyItem[size];
}
};
private MyItem(Parcel in) {
//mPosition = in.readLngLat ?
aString = in.readString();
}
}
第一个问题:如何在 writeToParcel 中写入 LngLat 字段以及如何在 MyItem(Parcel in) 构造函数中删除它?
第二个问题:这是否足够让这段代码可以工作?
ArrayList<MyItem> listItems = new ArrayList<>();
Iterator<MyItem> items = cluster.getItems().iterator();
while (items.hasNext()) {
listItems.add(items.next());
}
Intent intent = new Intent(MapsActivity.this, ClusterPosts.class);
intent.putParcelableArrayListExtra("oO", listItems);
startActivity(intent);
然后在 CluserPosts 中:
Intent intent = getIntent();
ArrayList<MyItem> post = intent.getParcelableArrayListExtra("oO");
for(MyItem item : post){
Log.d("elaela", item.getString());
}
你可以把经纬度写成double只写到目的地,然后形成一个位置。
dest.writeDouble(lat);
dest.writeDouble(long);
包裹写成-
dest.writeDouble(mPosition.latitude);
dest.writeDouble(mPosition.longitude);
我正在尝试将对象 MyItem 的 ArrayList 从一个 activity 传递到另一个。据我了解,我需要在 MyItem class 中实现 Parcable。这就是我到目前为止所做的:
public class MyItem implements ClusterItem, Parcelable {
private LatLng mPosition=null;
private String aString;
public MyItem(double lat, double lng, String aString) {
mPosition = new LatLng(lat, lng);
this.aString= aString;
}
@Override
public LatLng getPosition() {
return mPosition;
}
public String getString(){
return aString;
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
//dest.writeLngLat ?
dest.writeString(postID);
}
public static final Parcelable.Creator<MyItem> CREATOR
= new Parcelable.Creator<MyItem>() {
public MyItem createFromParcel(Parcel in) {
return new MyItem(in);
}
public MyItem[] newArray(int size) {
return new MyItem[size];
}
};
private MyItem(Parcel in) {
//mPosition = in.readLngLat ?
aString = in.readString();
}
}
第一个问题:如何在 writeToParcel 中写入 LngLat 字段以及如何在 MyItem(Parcel in) 构造函数中删除它?
第二个问题:这是否足够让这段代码可以工作?
ArrayList<MyItem> listItems = new ArrayList<>();
Iterator<MyItem> items = cluster.getItems().iterator();
while (items.hasNext()) {
listItems.add(items.next());
}
Intent intent = new Intent(MapsActivity.this, ClusterPosts.class);
intent.putParcelableArrayListExtra("oO", listItems);
startActivity(intent);
然后在 CluserPosts 中:
Intent intent = getIntent();
ArrayList<MyItem> post = intent.getParcelableArrayListExtra("oO");
for(MyItem item : post){
Log.d("elaela", item.getString());
}
你可以把经纬度写成double只写到目的地,然后形成一个位置。
dest.writeDouble(lat);
dest.writeDouble(long);
包裹写成-
dest.writeDouble(mPosition.latitude);
dest.writeDouble(mPosition.longitude);