传递一个可分辨的对象的arraylist

时间:2013-05-08 03:51:58

标签: android android-intent parcelable

我试图通过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);
      }
}

和prProduct:

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 weigth = 0.0

Recive: id = 7602278 weigth = 4.2E-44

2 个答案:

答案 0 :(得分:4)

副手我没有看到数据在传输中被破坏的地方,但它有助于清理代码。我个人修复了许多奇怪的错误,因为我重构了代码以提高可读性。你应该做的第一件事是删除“ParcelableProducts”类,因为你不需要它。只需使用putParcelableArrayListExtra方法直接传递intent中的ArrayList。 Shown here.

这也有点挑剔,但你不应该直接访问你的领域。最好将它们设置为私有并使用getter / setter。对于日志记录语句,每个循环使用a也会更清晰。

更安全的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());

答案 1 :(得分:0)

为什么需要创建实现ParcelableProducts的对象Parcelable?我想你可以直接使用意图的putParcelableArrayListExtra方法传递arraylist吗?