2016-11-16 71 views
2

我希望Activity2通过Intent接收对象(此对象具有其他对象的ArrayList)。 对象转移:通过意图传递其中的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); 

活性2接收对象:

Intent intent = getIntent(); 
     if (intent != null) { 
      bundle = intent.getParcelableExtra(ARG_BUNDLE); 
     } 

但是活性2只接收对象卡而不价格内部的ArrayList(对象价格也实现了Parcelable)。也许我做错了什么?

回答

2

你是不是在你的method.It阅读ArrayList的价格应该是:

protected Card(Parcel in) { 
     productName = in.readString(); 
     description = in.readString(); 
     prices= in.createTypedArrayList(Price.CREATOR); // add this line to your code. 
    } 
1
Intent i = getIntent(); 
stock_list = i.getStringArrayListExtra("stock_list"); 

发送端

Intent intent = new Intent(this, editList.class); 
     intent.putStringArrayListExtra("stock_list", stock_list); 
     startActivity(intent);