【问题标题】:Put two parcelable inside same intent将两个 parcelable 放在同一个意图中
【发布时间】:2016-05-03 20:52:15
【问题描述】:

伙计们,我对包裹很生气。 在这里,我有这些行:

Intent i = new Intent(context, PaymentDetailsActivity.class);
i.putExtra(ConstantsUtils.PARAM_INTENT_ORDER_DETAILS, responseOrderInfoApiModel);
i.putExtra(ConstantsUtils.PARAM_INTENT_PAYMENT_INFO, paymentInfoViewModel);
startActivity(i);

我在这里收到我的包裹:

Intent receivedIntent = getIntent();
responseOrderInfoApiModel = receivedIntent.getParcelableExtra(ConstantsUtils.PARAM_INTENT_ORDER_DETAILS);
paymentInfoViewModel = receivedIntent.getParcelableExtra(ConstantsUtils.PARAM_INTENT_PAYMENT_INFO);

重点是,responseOrderInfoApiModel 正在接收 null,但它不应该,但是如果我评论该行:

i.putExtra(ConstantsUtils.PARAM_INTENT_PAYMENT_INFO, paymentInfoViewModel);

我的responseOrderInfoApiModel 收到正确的值。

这是我的包裹:

ResponseOrderInfoApiModel -

public class ResponseOrderInfoApiModel implements Parcelable{

    private String cardNumber;
    private double total;
    private ArrayList<ResponseOrderItemApiModel> listItem;

    public ResponseOrderInfoApiModel() {
    }

    public ResponseOrderInfoApiModel(Parcel source) {
        if(listItem == null){
            listItem = new ArrayList<>();
        }

        setCardNumber(source.readString());
        setTotal(source.readDouble());
        source.readTypedList(listItem, ResponseOrderItemApiModel.CREATOR);
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(cardNumber);
        dest.writeDouble(total);
        dest.writeTypedList(listItem);
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        @Override
        public ResponseOrderInfoApiModel createFromParcel(Parcel source) {
            return new ResponseOrderInfoApiModel(source);
        }

        @Override
        public ResponseOrderInfoApiModel[] newArray(int size) {
            return new ResponseOrderInfoApiModel[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    public ArrayList<ResponseOrderItemApiModel> getListItem() {
        return listItem;
    }

    public void setListItem(ArrayList<ResponseOrderItemApiModel> listItem) {
        this.listItem = listItem;
    }

    public String getCardNumber() {
        return cardNumber;
    }

    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }

    public double getTotal() {
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }
}

ResponseOrderItemApiModel(是ResponseOrderInfoApiModel中的arrayList)-

public class ResponseOrderItemApiModel implements Parcelable {

    private String description;
    private int quantity;
    private double total;
    private double unitPrice;

    public ResponseOrderItemApiModel() {
    }

    public ResponseOrderItemApiModel(Parcel source) {
        setDescription(source.readString());
        setQuantity(source.readInt());
        setTotal(source.readDouble());
        setUnitPrice(source.readDouble());
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(description);
        dest.writeInt(quantity);
        dest.writeDouble(total);
        dest.writeDouble(unitPrice);
    }

    public static Parcelable.Creator<ResponseOrderItemApiModel> CREATOR = new Parcelable.Creator<ResponseOrderItemApiModel>(){
        @Override
        public ResponseOrderItemApiModel createFromParcel(Parcel source) {
            return new ResponseOrderItemApiModel(source);
        }

        @Override
        public ResponseOrderItemApiModel[] newArray(int size) {
            return new ResponseOrderItemApiModel[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public double getTotal() {
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }

    public double getUnitPrice() {
        return unitPrice;
    }

    public void setUnitPrice(double unitPrice) {
        this.unitPrice = unitPrice;
    }
}

PaymentInfoViewModel -

public class PaymentInfoViewModel implements Parcelable {

    private long idEstablishment;
    private String nameEstablishment;
    private int cardNumber;
    private double cardSubTotalValue;
    private double cardTotalValue;
    private byte tipPercentage;
    private long idCardPayment;

    public PaymentInfoViewModel() {
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        @Override
        public PaymentInfoViewModel createFromParcel(Parcel source) {
            return new PaymentInfoViewModel(source);
        }

        @Override
        public PaymentInfoViewModel[] newArray(int size) {
            return new PaymentInfoViewModel[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(idEstablishment);
        dest.writeString(nameEstablishment);
        dest.writeInt(cardNumber);
        dest.writeDouble(cardSubTotalValue);
        dest.writeDouble(cardTotalValue);
        dest.writeByte(tipPercentage);
        dest.writeLong(idCardPayment);
    }

    public PaymentInfoViewModel(Parcel source) {
        setIdEstablishment(source.readLong());
        setNameEstablishment(source.readString());
        setCardNumber(source.readInt());
        setCardSubTotalValue(source.readDouble());
        setCardTotalValue(source.readDouble());
        setTipPercentage(source.readByte());
        setIdCardPayment(idCardPayment);
    }

    public long getIdEstablishment() {
        return idEstablishment;
    }

    public void setIdEstablishment(long idEstablishment) {
        this.idEstablishment = idEstablishment;
    }

    public String getNameEstablishment() {
        return nameEstablishment;
    }

    public void setNameEstablishment(String nameEstablishment) {
        this.nameEstablishment = nameEstablishment;
    }

    public int getCardNumber() {
        return cardNumber;
    }

    public void setCardNumber(int cardNumber) {
        this.cardNumber = cardNumber;
    }

    public double getCardTotalValue() {
        return cardTotalValue;
    }

    public void setCardTotalValue(double cardTotalValue) {
        this.cardTotalValue = cardTotalValue;
    }

    public byte getTipPercentage() {
        return tipPercentage;
    }

    public void setTipPercentage(byte tipPercentage) {
        this.tipPercentage = tipPercentage;
    }

    public double getCardSubTotalValue() {
        return cardSubTotalValue;
    }

    public void setCardSubTotalValue(double cardSubTotalValue) {
        this.cardSubTotalValue = cardSubTotalValue;
    }

    public long getIdCardPayment() {
        return idCardPayment;
    }

    public void setIdCardPayment(long idCardPayment) {
        this.idCardPayment = idCardPayment;
    }
}

【问题讨论】:

  • 也许ConstantsUtils.PARAM_INTENT_PAYMENT_INFOConstantsUtils.PARAM_INTENT_ORDER_DETAILS 具有相同的值,而paymentInfoViewModelnull
  • 我希望是那个,但不是:public static final String PARAM_INTENT_PAYMENT_INFO = "Param_PaymentInfo";public static final String PARAM_INTENT_ORDER_DETAILS = "Param_OrderDetails";
  • 不确定是不是这个,但是你在listItem ArrayList 中读到的,我的做法是listItem = source.createTypedArrayList(ResponseOrderItemApiModel.CREATOR);

标签: android android-intent parcelable


【解决方案1】:

尝试使用 bundle 并将您的数据放入 bundle 中,然后将 bundle 放入 Intent 中。一旦我遇到了这么一个有趣的问题,这种方法就解决了我的问题。

【讨论】:

  • 即使使用bundle,也会出现同样的问题! :/如果我检查(使用调试器),意图是正确的,看起来内部的东西正在使该对象为空。
猜你喜欢
  • 2011-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-16
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
相关资源
最近更新 更多