Java 包裹对象中的数据覆盖

Java 包裹对象中的数据覆盖,java,android,parcelable,Java,Android,Parcelable,在我的应用程序中,我的数据未正确写入我的可包裹对象。我正在将可包裹对象列表从一个活动传递到另一个活动,但数据写入不正确,被覆盖。 例如,我的列表中只有一个项目,我在列表中添加了“价格=10”,当我在另一个活动中尝试阅读此列表时,我得到了正确的价格,即“10”,但当我尝试在此列表中添加一个项目时,假设“价格=20”,当我尝试阅读此列表时,我得到了两个价格=20且价格=20的项目。在这种情况下,我的数据被覆盖 这是我的密码 可包裹对象: package com.example.model;

在我的应用程序中,我的数据未正确写入我的可包裹对象。我正在将可包裹对象列表从一个活动传递到另一个活动,但数据写入不正确,被覆盖。 例如,我的列表中只有一个项目,我在列表中添加了“价格=10”,当我在另一个活动中尝试阅读此列表时,我得到了正确的价格,即“10”,但当我尝试在此列表中添加一个项目时,假设“价格=20”,当我尝试阅读此列表时,我得到了两个价格=20且价格=20的项目。在这种情况下,我的数据被覆盖

这是我的密码

可包裹对象:

  package com.example.model;

 import java.util.ArrayList;
  import java.util.List;

   import android.os.Parcel;
   import android.os.Parcelable;

 public class Model_MiscBarcode implements Parcelable {

public String fixedPrice, price, quantity, string_sale_return, barcode;

public Model_MiscBarcode() {
    fixedPrice = "";
    price = "";
    quantity = "";
    string_sale_return = "";
    barcode = "";
}

public Model_MiscBarcode(Parcel in) {
    fixedPrice = in.readString();
    price = in.readString();
    quantity = in.readString();
    string_sale_return = in.readString();
    barcode = in.readString();
}

public String getString_sale_return() {
    return string_sale_return;
}

public void setString_sale_return(String string_sale_return) {
    this.string_sale_return = string_sale_return;
}

public String getBarcode() {
    return barcode;
}

public void setBarcode(String barcode) {
    this.barcode = barcode;
}

public String getQuantity() {
    return quantity;
}

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

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

public String getFixedPrice() {
    return fixedPrice;
}

public void setFixedPrice(String fixedPrice) {
    this.fixedPrice = fixedPrice;
}



@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(fixedPrice);
    dest.writeString(price);
    dest.writeString(quantity);
    dest.writeString(string_sale_return);
    dest.writeString(barcode);

}

public static final Parcelable.Creator<Model_MiscBarcode> CREATOR = new   
Parcelable.Creator<Model_MiscBarcode>() {
    public Model_MiscBarcode createFromParcel(Parcel in) {
        return new Model_MiscBarcode(in);
    }

    public Model_MiscBarcode[] newArray(int size) {
        return new Model_MiscBarcode[size];
    }
};
}

根据您的代码,只有一个
miscBarcodeList
实例。你只是一遍又一遍地覆盖它。尝试在
insertNewBarcode()
方法内初始化
miscBarcodeList

可以共享创建对象miscBarcodeList的位置吗
 private void insertNewBarcode(String barcode) {

        if(data.getBarcode() == null || 
              data.getBarcode().equalsIgnoreCase("")){
            data.setBarcode("000000000000");
        }else{
        data.setBarcode(barcode);
        }
        data.setQuantity(Integer.toString(1));
        data.setString_sale_return(value);
        double price = Double.parseDouble(data.getPrice());
        double quantity = Double.parseDouble(data.getQuantity());

        String pAmount = decimalFormat.format(price);
        pAmount = String.format(pAmount);
        data.setPrice(pAmount);
        data.setFixedPrice(String.valueOf(new DecimalFormat("0.00")
                .format(price)));
        miscBarcodeList.setBarcode(data.getBarcode());
        miscBarcodeList.setFixedPrice(data.getFixedPrice());
        miscBarcodeList.setPrice(data.getPrice());
        miscBarcodeList.setQuantity(data.getQuantity());
        miscBarcodeList.setString_sale_return(data.getString_sale_return());
        miscList.add(miscBarcodeList);
 }