Search code examples
javaandroidparcelable

Data overwriting in Parcelable Object Android


In my App my data is not writing correctly in my Parcelable object. I am passing List of Parcelable objects from one activity to another but the data is not writing properly, its overwriting. For eg. There is only 1 item in my List, I add "price = 10" my List and when I try to read this list in the another activity, I am getting correct price i.e "10" BUT when I try to add one more item in this List, suppose "price = 20" and when I try to read this List I get 2 items with price = 20 and price =20. In this case my data overwrites.

Heres my code.

PARCELABLE OBJECT:

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

Here's my code where I'm trying to add data in my List:

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

Solution

  • Based on your code, there is only 1 instance of miscBarcodeList. You just override it over and over. Try to initialize miscBarcodeList inside insertNewBarcode() method.