Android Parcelable对象返回Null

时间:2011-09-13 10:08:11

标签: android

我有Product class。我想将产品对象的一个​​活动传递给另一个。

我的实施方式如下:

 public class Product implements Parcelable{
     private double availableQuantity;
     private double price;
     private String productCode;    
     private String description;
     private String nonStockItemFlag;   
     private String activeFlag;
     private String kitProductFlag;
     private double value;
     private ArrayList<Product> product;
     private double qty;

    public Product() {

}


/**
 * @param availableQuantity
 * @param price
 * @param productCode
 * @param description
 * @param nonStockItemFlag
 * @param kitProductFlag
 * @param qty
 * @param grossValue
 * @param value
 */

public Product(double availableQuantity, double price, String productCode,
        String description, String nonStockItemFlag, String kitProductFlag,
        double qty,  double value) {
    super();
    this.availableQuantity = availableQuantity;
    this.price = price;
    this.productCode = productCode;
    this.description = description;
    this.nonStockItemFlag = nonStockItemFlag;
    this.kitProductFlag = kitProductFlag;
    this.qty = qty;
    this.value = value;
}
     // setter & getter


public int describeContents() {
    return 0;
}


public void writeToParcel(Parcel dest, int flags) {

    Bundle b = new Bundle();
    b.putParcelableArrayList("enteredProduct", product);
    dest.writeBundle(b);
}


public static final Parcelable.Creator<Product> CREATOR = new Parcelable.Creator<Product>() { 
    public Product createFromParcel(Parcel in) { 
        Product prod = new Product();

    Bundle b = in.readBundle(Product.class.getClassLoader());        
    prod.product = b.getParcelableArrayList("enteredProduct");
    System.out.println("***product***" + prod.product.get(0).getPrice());
    return prod;
    }

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

这是来电者:

   if(productMap.size() >0){
         ArrayList<Product> enteredProductList = new ArrayList<Product>(productMap.values());
         System.out.println("-enteredProductList --" + enteredProductList.size());
         System.out.println("--- " +enteredProductList.get(0).getPrice() );
         Bundle b = new Bundle();
         b.putParcelableArrayList("enteredProduct", enteredProductList);
         Intent showContent = new Intent(getApplicationContext(),RetailerOrderIActivity.class);
         showContent.putExtras(b); //Insert the Bundle object in the Intent' Extras
         startActivity(showContent);
     }else{
         Toast.makeText(RetailerOrderActivity.this," You don't have invoice records" ,Toast.LENGTH_SHORT).show();
     }

这是接收部分:

    Bundle b = this.getIntent().getExtras();
   ArrayList<Product> p = b.getParcelableArrayList("enteredProduct");
    System.out.println("-- RetailerOrderIActivity --" + p.size() );
    for(Product s : p){
    System.out.println(" --Qty-" + s.getQty());
    System.out.println(" --price -" + s.getPrice());
    System.out.println(" --code -" + s.getProductCode());
    }

接收部分返回null值。但是发送活动部分包含值。请更正我的代码吗?

我的代码有什么问题?

我有很多产品实体calss的属性。但我想设置一些实体。

提前致谢。

1 个答案:

答案 0 :(得分:10)

我猜你误解了Parcelable的例子。 您必须将所有需要的元素放入宗地,然后再从中获取:

要将对象写入宗地,需要这样做:

public void writeToParcel(Parcel dest, int flags) 
{
    dest.writeString(productCodce);
    dest.writeString(description);
            // and all other elements
}

另外,您需要一个接收包裹的构造函数:

public Product(Parcel in)
{
    this.productCode=in.readString();
    this.description=in.readString();
            // and all other elements
    }

你的创造者应该是这样的:

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() 
{
    public Product createFromParcel(Parcel in) { return new Product(in); }
    public Product[] newArray(int size) { return new Product[size]; }
};

现在,在您的活动级别(不在您的产品类中!):

将其推入额外内容,使用:

bundle.putParcelableArrayList("whatevername",products);

要从附加内容中获取它,请使用简单的:

ArrayList<Product> products=bundle.getParcelableArrayList("whatevername");