gpt4 book ai didi

java - 通过 Intent 传递 Bundle

转载 作者:行者123 更新时间:2023-12-02 07:32:02 26 4
gpt4 key购买 nike

我正在尝试将数据存储到一个包中并开始一个新的 Activity 。然后在新 Activity 中我想取消数据转换。类型转换部分的代码如下:

Intent i = new Intent(MainActivity.this, ShowProductActivity.class);
Bundle bundle = new Bundle();
// Getting adapter of the listview
SimpleAdapter adapter = (SimpleAdapter ) mListView.getAdapter();
bundle.putSerializable("param1", ((HashMap<String, Object>) adapter.getItem(position)));
i.putExtras(bundle);
startActivity(i);

一般来说,bundle中包含的数据如下:

Bundle[{param1={position=0, image=2130837504, flag=/data/data/com.example.app/cache/wpta_0.jpeg, price=Brand : Dash Price : 27}}]

如何将上述数据检索到第二个 Activity 中?我正在使用以下代码块,但无法访问属性。

Bundle bundle = this.getIntent().getExtras();
HashMap<String, Object> param1 = (HashMap<String, Object>)bundle.getSerializable("param1");

最佳答案

我认为您不需要创建一个 bundle 来传递一个可序列化,只需将可序列化直接放入 Intent extras 中即可:

i.putExtras("param1", ((HashMap<String, Object>) adapter.getItem(position)));
<小时/>

或者如果您必须使用中间 bundle ,请查看 Bundle#putExtras(Bundle bundle) 的文档:

Add a set of extended data to the intent. The keys must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".

关于java - 通过 Intent 传递 Bundle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12790722/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com