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的屬性,但我想設置一些實體。
在此先感謝。
錯過了一件事: 因此,現在如果您的Product類包含要添加的產品的ArrayList,但無法在Parcelable中推送Parcelable,則需要考慮另一種傳輸此數據的方法。這不可能。 – Oliver
請同時參考http://stackoverflow.com/questions/1712557/using-parcelable-with-circular-references或http://stackoverflow.com/questions/3513665/problem-in-implementing-parcelable-containing-other -parcelable – Oliver
是的。它工作正常 – Piraba