1

我试图通过 Parcelable 传递对象数组。但是在传输之后,数据被转换成奇怪的东西

传输部分:

for (int i=0; i<res.size(); i++) {
                Log.d(LOG_TAG, "id = "+res.get(i).id+" weigth = "+res.get(i).weight); 
            }

            ParcelableProducts Checked = new ParcelableProducts();
            Checked.setList(res);
            intent.putExtra(ParcelableProducts.class.getCanonicalName(), Checked);

接收部分:

ParcelableProducts res = (ParcelableProducts) data.getParcelableExtra(ParcelableProducts.class.getCanonicalName());
            ArrayList<prProduct> prod = res.prod;
            for (int i=0; i<prod.size(); i++) {
                Log.d(LOG_TAG, "id = "+prod.get(i).id+" weigth = "+prod.get(i).weight); 
            }

可使用 ArrayList 打包的类:

public class ParcelableProducts implements Parcelable {
    final private static String LOG_TAG = "ParcelableProducts";
    public ArrayList<prProduct> prod;

    public ParcelableProducts() {
        prod = new ArrayList<prProduct>();
     }

    public void setList(ArrayList<prProduct> _prod){
        prod = _prod;
    }

    public ArrayList<prProduct> getList() {
        return prod;
    }

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

    @Override
    public void writeToParcel(Parcel parcel, int flags) {
        parcel.writeList(prod);
    }
    public static final Parcelable.Creator<ParcelableProducts> CREATOR = new Parcelable.Creator<ParcelableProducts>() {
        public ParcelableProducts createFromParcel(Parcel in) {
          return new ParcelableProducts(in);
        }

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

      private ParcelableProducts(Parcel parcel) {
        prod  = new ArrayList<prProduct>();
        parcel.readTypedList(prod, prProduct.CREATOR);
      }
}

和公关产品:

public class prProduct implements Parcelable {
      final static String LOG_TAG = "prProduct";

      public float weight;
      public int id;

      public prProduct(int _id, Float _weight) {
        weight = _weight;
        id = _id;
      }

      public int describeContents() {
        return 0;
      }

      public void writeToParcel(Parcel parcel, int flags) {
        parcel.writeFloat(weight);
        parcel.writeInt(id);
      }

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

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

      private prProduct(Parcel parcel) {
        weight = parcel.readFloat();
        id = parcel.readInt();
      }
}

在日志中:

传输: id = 7 重量 = 0.0

接收: id = 7602278 重量 = 4.2E-44

4

2 回答 2

4

我看不到数据在传输中损坏的地方,但它有助于清理您的代码。就个人而言,我过去在重构代码以提高可读性时修复了许多奇怪的错误。您应该做的第一件事是删除“ParcelableProducts”类,因为您不需要它。只需使用 putParcelableArrayListExtra 方法直接传递 Intent 中的 ArrayList。 显示在这里。

这也有点挑剔,但您不应该直接访问您的字段。最好将它们设置为私有并使用 getter/setter。此外,为您的日志记录语句使用 for each 循环会更干净一些。

更安全的 PrProduct 类。

//Java classes should start with capital letter
public class PrProduct implements Parcelable {
    private final static String LOG_TAG = "PrProduct";

    private float weight;
    private int id;

    public prProduct(int id, float weight) {
        this.weight = weight;
        this.id = id;
    }
    //Using getters only makes this immutable which is safer since the 
    //weight/id aren't likely to change.
    public float getWeight(){
        return weight;}
    public int getId(){
        return id;}

    public int describeContents() {
      return 0;
    }

    public void writeToParcel(Parcel parcel, int flags) {
      parcel.writeFloat(weight);
      parcel.writeInt(id);
    }

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

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

    private prProduct(Parcel parcel) {
      weight = parcel.readFloat();
      id = parcel.readInt();
    }
}

//A sample for each loop
for(PrProduct product: prod)
    Log.d(LOG_TAG, "weight=" + product.getWeight() + " id=" + product.getId());
于 2013-05-08T04:19:01.243 回答
0

为什么需要创建ParcelableProducts实现的对象Parcelable?我认为您可以直接使用putParcelableArrayListExtra意图中的方法传递数组列表吗?

于 2013-05-08T06:05:38.223 回答