2016-11-16 14 views
2

Intentを介してActivity2にオブジェクト(このオブジェクトは他のオブジェクトのArrayListがあります)を受け取るようにします。 オブジェクトを転送します:Intraentを介して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は、内部価格のArrayListの(オブジェクトなしオブジェクトのみカードを受け取ります価格もParcelableを実装します)。多分私は何か間違っているのですか?

答えて

2

を送信すると、次のようになります。

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"); 

あなたのmethod.Itで価格のArrayListを読んでいないエンド

Intent intent = new Intent(this, editList.class); 
     intent.putStringArrayListExtra("stock_list", stock_list); 
     startActivity(intent); 
関連する問題