把两个parcelable放在同一个意图中

时间:2016-05-03 20:52:15

标签: android android-intent parcelable

伙计们,我对于parcelables感到很生气。 在这里,我有这些线:

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

在这里,我收到了我的parcelables:

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

关键是,responseOrderInfoApiModel正在接收空值,但它不应该,但是如果我对该行进行注释:

i.putExtra(ConstantsUtils.PARAM_INTENT_PAYMENT_INFO, paymentInfoViewModel);

我的responseOrderInfoApiModel收到正确的值。

这是我的parcelables:

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;
    }
}

1 个答案:

答案 0 :(得分:0)

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