Android的Parcelable对象返回null对象、Android、Parcelable、null

2023-09-06 21:54:13 作者:随心录红尘

我有产品class.I想通过产品对象一个活动到另一个。

I have Product class.I want to pass product object one activity to another.

我已经实现了这样的:

 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改正我的code?

The receiving part return null value.But in the sending Activity part contain value.Please correct my code?

什么是错在我的code?

what is wrong in my code?

我有很多产权产品实体calss.But我想设置一些实体。

I have lot of property for product entity calss.But i want to set some of entity.

在此先感谢。

推荐答案

我想你误会了Parcelable例子。 你必须从它把所有需要的元素成包裹,并得到它以后:

I guess you misunderstood the Parcelable examples. You have to put all needed elements into the parcel, and get it from it later:

要编写对象的包裹,这是必要的:

To write your object to the parcel, this is needed:

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

此外,你需要一个构造函数接收包裹:

Plus, you need a constructor receiving a parcel:

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

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

Your Creator should be something like:

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]; }
};

现在,在你的活动水平(而不是在你的产品类!):

Now, at your activity level (NOT in your Product class!):

将其推入额外的,使用方法:

Push it into extras, use:

bundle.putParcelableArrayList("whatevername",products);

要在额外得到它,使用简单:

To get it from the extras, use a simple:

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