[英]Android - Parcelable | putParcelableArrayListExtra("ListItems", listItems);
I am trying to pass an ArrayList of objects MyItem from one activity to another.我正在尝试将对象 MyItem 的 ArrayList 从一个活动传递到另一个活动。 From what i understand i need to implement Parcable in MyItem class.据我了解,我需要在 MyItem 类中实现 Parcable。 So this is what i'va done so far:所以这就是我到目前为止所做的:
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();
}
}
First question : How could i write LngLat field in writeToParcel and how could i decleare it in MyItem(Parcel in) constructor?第一个问题:我怎么能在 writeToParcel 中写 LngLat 字段,我怎么能在 MyItem(Parcel in) 构造函数中清除它?
Second question : Is this going to be enough so this piece of code could work?第二个问题:这是否足够让这段代码可以工作?
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);
and then in CluserPosts:然后在 CluserPosts 中:
Intent intent = getIntent();
ArrayList<MyItem> post = intent.getParcelableArrayListExtra("oO");
for(MyItem item : post){
Log.d("elaela", item.getString());
}
Write parcel as -将包裹写为 -
dest.writeDouble(mPosition.latitude);
dest.writeDouble(mPosition.longitude);
You can write latitude and longitude as doubles only to the destination and then form a position.您可以仅将纬度和经度写为双倍到目的地,然后形成一个位置。
dest.writeDouble(lat);
dest.writeDouble(long);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.