[英]Passing object with arraylist inside of it through Intent
我希望Activity2通過Intent接收對象(此對象內部具有其他對象的ArrayList)。 轉讓對象:
public class Card implements Parcelable {
@SerializedName("product_name")
private String productName;
private String description;
private List<Price> prices;
public Card() {
}
public Card(String productName, String description, List<Price> prices) {
this.productName = productName;
this.description = description;
this.prices = prices;
}
protected Card(Parcel in) {
productName = in.readString();
description = in.readString();
}
public static final Creator<Card> CREATOR = new Creator<Card>() {
@Override
public Bundle createFromParcel(Parcel in) {
return new Card(in);
}
@Override
public Card[] newArray(int size) {
return new Card[size];
}
};
public String getProductName() {
return productName;
}
public String getDescription() {
return description;
}
public List<Price> getPrices() {
return prices;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(productName);
dest.writeString(description);
dest.writeTypedList(prices);
} }
意圖(在片段內部):
Intent intent = new Intent(getActivity(), Activity2.class);
intent.putExtra(Activity2.ARG_BUNDLE, card);
startActivity(intent);
Activity2接收對象:
Intent intent = getIntent();
if (intent != null) {
bundle = intent.getParcelableExtra(ARG_BUNDLE);
}
但是Activity2僅接收對象,而內部沒有Price的ArrayList(對象Price也實現了Parcelable)。 也許我做錯了什么?
您沒有在您的方法中讀取價格數組列表,應該是:
protected Card(Parcel in) {
productName = in.readString();
description = in.readString();
prices= in.createTypedArrayList(Price.CREATOR); // add this line to your code.
}
Intent i = getIntent();
stock_list = i.getStringArrayListExtra("stock_list");
發送端
Intent intent = new Intent(this, editList.class);
intent.putStringArrayListExtra("stock_list", stock_list);
startActivity(intent);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.