Android Parcelable 对象返回 Null

2023-12-03

我有产品类。我想将产品对象从一项活动传递到另一项活动。

我已经这样实现了:

 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());
    }

接收部分返回空值。但在发送活动部分包含值。请更正我的代码?

我的代码有什么问题吗?

我有很多产品实体类的属性。但我想设置一些实体。

提前致谢。


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

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

将其推入 extras,使用:

bundle.putParcelableArrayList("whatevername",products);

要从 extras 中获取它,请使用简单的:

ArrayList<Product> products=bundle.getParcelableArrayList("whatevername");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android Parcelable 对象返回 Null 的相关文章

随机推荐

  • Java DOM 通过 ID 获取元素

    我正在 Java 中使用 DOM 解析器将子节点添加到现有节点中 我的 XML 是
  • 如何查找 SQL Server Management Studio 的服务器名称

    我安装了 Microsoft SQL Server 2008 当我启动 SQL Server Management Studio SSMS 时 我得到Connect to Server带有空白文本框的登录窗口Server name 我尝试了
  • 在 mysql 8 中使用 GROUP BY

    我已经升级了 MySQL 服务器 现在我遇到了 group by 问题 select from tickets WHERE status 0 group by tnumber ORDER BY created at DESC 错误是 SEL
  • mllib NaiveBayes 中的类数量有限制吗?调用 model.save() 时出错

    我正在尝试训练一个模型来预测文本输入数据的类别 我使用以下方法遇到了似乎数值不稳定的问题pyspark ml classification NaiveBayes当类别数量超过一定数量时 对词袋进行分类 在我的现实世界项目中 我有大约 10
  • Javascript for 循环和警报

    我正在循环浏览链接列表 我可以正确获取标题属性 并希望它在单击时显示 当页面加载并单击链接时 所有链接标题都会一一提醒 我究竟做错了什么 function prepareShowElement var nav document getEle
  • 仅在 apex 报告的某些行上显示“编辑”链接

    我有以下报告结构 显示全年产品的销售目标和销售额 每个产品都有两行 一行显示销售额 一行显示 sales target 目前 我的报告在报告的每一行旁边都有一个编辑链接 有没有办法从 sales target 行中删除编辑链接 select
  • 无法使用 PRAW 从某些 Reddit 子版块中获取随机帖子

    我正在尝试使用此代码让一个不和谐的机器人从reddit发送图像 只是为了抓取而不是发送 def random post subreddit while True post reddit subreddit subreddit random
  • Android:如何获取自定义View的高度和宽度? [复制]

    这个问题在这里已经有答案了 我该如何使用getMeasuredWidth and getMeasuredHeight 它总是返回 0 这和有什么区别getHeight and getWidth 刚刚找到了获取自定义视图的高度和宽度的解决方案
  • SQLite中保存资源路径

    我需要将图像保存在 SQLite 数据库的资源文件夹中 该数据库是预加载的 复制到数据 路径 因此在运行时不会填充 我尝试将其保存在 SQLite 的文本字段中 R drawable picture1 我的drawable文件夹中有大约30
  • MySQL FULLTEXT 不适用于多个字段

    我有一个标准的 MySQL 全文查询 如下所示 SELECT FROM accounts WHERE MATCH first name username AGAINST stringhere 问题是 它找不到该字段的全文索引first na
  • 通过 jQuery 添加 JSF 标签/组件

    这是我所做的 var a document getElementById panelForm tableId01
  • NSIS 系统 kernel32::LoadLibrary 不搜索 Outdir 或 Path

    我正在尝试在 NSIS 安装程序中加载并调用 C 库 DLL 的函数 当我尝试加载 DLL 时 发出错误 126 ERROR MOD NOT FOUND 这是我用来测试的最小安装程序脚本 OutFile Main exe ShowInstD
  • 如何配置Json.NET自定义序列化?

    由于我无法控制的原因 我从外部服务返回的数据被格式化为字符串数组的数组 string one string two 我试图将其反序列化为具有两个属性的对象 public class MyObject public string Proper
  • ajax 调用导致错误而不是成功

    在我的 ASP net mvc3 项目中 我使用 ajax 调用将 json 数据发送到控制器公司中的创建操作方法 但是当我调试ajax调用时 它总是以错误结果而不是成功结果结束 阿贾克斯调用 ajax url Company Create
  • 在javascript中移动对象元素位置

    我正在尝试找到一种将对象元素移动到特定位置的方法 例如 我有这个对象 element1 object element2 element3 我搜索将 element3 移动到第一个位置 element3 element1 element2 感
  • 从 PDO 准备好的语句中获取原始 SQL 查询字符串

    在准备好的语句上调用 PDOStatement execute 时 有没有办法让原始 SQL 字符串执行 出于调试目的 这将非常有用 我假设您的意思是您想要最终的 SQL 查询 并将参数值插入其中 我知道这对于调试很有用 但这不是准备好的语
  • 如何为等待函数调用添加超时

    向等待函数添加超时的最佳方法是什么 Example lets pretend this is in a library that I m using and I can t mess with the guts of this thing
  • 按平均值排名的箱线图

    我想显示多个变量的箱线图 并按列均值降序排列它们 就像在绩效分析包裹 我使用以下代码生成箱线图 zx lt replicate 5 rnorm 50 zx means lt colMeans zx na rm TRUE boxplot zx
  • django 和 facebook:代表用户执行第三方登录的 facebook web 应用程序的安全性和设计

    我正在编写一个 Facebook 画布 Web 应用程序 它执行对第三方网站的登录 使用 urllib 并代表用户执行操作 这意味着我有2个账户 用户在我的网络应用程序中拥有的帐户 通过 facebook 以及应用程序用来代表他们执行登录的
  • Android Parcelable 对象返回 Null

    我有产品类 我想将产品对象从一项活动传递到另一项活动 我已经这样实现了 public class Product implements Parcelable private double availableQuantity private