java - Parcelable Object Android 中的数据覆盖

标签 java android parcelable

在我的应用程序中,我的数据没有正确写入我的 Parcelable 对象。我正在将 Parcelable 对象列表从一个 Activity 传递到另一个 Activity ,但数据没有正确写入,它被覆盖了。 例如。我的列表中只有 1 个项目,我在列表中添加了“price = 10”,当我尝试在另一个 Activity 中阅读此列表时,我得到了正确的价格,即“10”,但是当我尝试在其中添加一个项目时这个列表,假设“price = 20”,当我尝试阅读这个列表时,我得到 2 个 price = 20 和 price =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];
    }
};
}

这是我尝试在我的列表中添加数据的代码:

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

最佳答案

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

关于java - Parcelable Object Android 中的数据覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37057276/

相关文章:

java - streaming jar 找不到 HDFS 中的 Mapper 文件

java - 如何让应用保持存活?运行后出现奇怪的错误

java - lambda 表达式中的错误返回类型 : BigDecimal cannot be converted to long

java - Spring boot 无法从 AWS EC2 中提取环境变量

java - RxJava - 合并两个 Observable

android - 尝试解包 ConcurrentHashMap 时出现 ClassCastException

Android相机 Intent 在拍摄肖像时保存图像风景

c# - Android gmail gif 闪烁

android - 如何在 Android 中将对象传递给新的 Intent

android - 在库类上使用 Parcelable