Android Parcelable对象返回Null

Android Parcelable对象返回Null,第1张

概述我有Productclass.我想将产品对象的一个​​活动传递给另一个.我这样实现了:publicclassProductimplementsParcelable{privatedoubleavailableQuantity;privatedoubleprice;privateStringproductCode;privateStringdescription;

我有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 & getterpublic 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的属性.但我想设置一些实体.

提前致谢.

解决方法:

我猜你误解了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]; }};

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

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

bundle.putParcelableArrayList("whatevername",products);

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

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

以上是内存溢出为你收集整理的Android Parcelable对象返回Null全部内容,希望文章能够帮你解决Android Parcelable对象返回Null所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1115938.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存