2011-09-13 110 views
3

我有产品类。我想将产品对象一个活动传递给另一个。Android Parcelable对象返回空

我已经实现了这样的:

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 value.But在发送活动部分包含value.Please更正我的代码?

我的代码有什么问题?

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

在此先感谢。

回答

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"); 
+0

错过了一件事: 因此,现在如果您的Product类包含要添加的产品的ArrayList,但无法在Parcelable中推送Parcelable,则需要考虑另一种传输此数据的方法。这不可能。 – Oliver

+0

请同时参考http://stackoverflow.com/questions/1712557/using-parcelable-with-circular-references或http://stackoverflow.com/questions/3513665/problem-in-implementing-parcelable-containing-other -parcelable – Oliver

+0

是的。它工作正常 – Piraba