我想從一個活動傳遞一個對象MyItem的ArrayList到另一個。從我理解我需要在MyItem類中實現Parcable。因此,這是i'va迄今所做的:Android - Parcelable | putParcelableArrayListExtra(「ListItems」,listItems);
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();
}
}
第一個問題:我怎麼能寫LngLat領域writeToParcel,我怎麼能decleare它MyItem(包裹中)構造函數?
第二個問題:這是否足以讓這段代碼可以工作?
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());
}
'LatLng'是'Parcelable',對不對? – pskink
LatLng是Google Maps API的「默認」對象,未由我實施。但是我認爲它實際上可以是 –
,所以它是'Parcelable',然後直接從/到'Parcel'讀/寫它 – pskink