2011-09-13 140 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