【问题标题】:Passing object with arraylist inside of it through Intent通过 Intent 传递其中包含 arraylist 的对象
【发布时间】:2016-11-16 09:06:46
【问题描述】:

我想让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(Fragment 内部):

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 的对象 Card(对象 Price 也实现了 Parcelable)。也许我做错了什么?

【问题讨论】:

    标签: java android android-intent parcelable


    【解决方案1】:

    您没有在您的方法中读取价格数组列表。应该是:

    protected Card(Parcel in) {
            productName = in.readString();
            description = in.readString();
            prices= in.createTypedArrayList(Price.CREATOR); // add this line to your code.
        }
    

    【讨论】:

      【解决方案2】:
      Intent i = getIntent();  
      stock_list = i.getStringArrayListExtra("stock_list");
      

      发送结束

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多