如何使用意图将哈希图值发送到另一个活动 [英] How to send hashmap value to another activity using an intent
问题描述
如何将 HashMap
值从一个 Intent 发送到第二个 Intent?
How to send HashMap
value from one Intent to second Intent?
另外,如何在第二个 Activity 中检索 HashMap
值?
Also, how to retrieve that HashMap
value in the second Activity?
推荐答案
Java 的 HashMap 类扩展了 Serializable
接口,使用 Intent.putExtra(String, Serializable)
方法.
Java's HashMap class extends the Serializable
interface, which makes it easy to add it to an intent, using the Intent.putExtra(String, Serializable)
method.
在接收到意图的活动/服务/广播接收器中,然后调用Intent.getSerializableExtra(String)
使用您在 putExtra 中使用的名称.
In the activity/service/broadcast receiver that receives the intent, you then call
Intent.getSerializableExtra(String)
with the name that you used with putExtra.
例如发送intent时:
For example, when sending the intent:
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("key", "value");
Intent intent = new Intent(this, MyOtherActivity.class);
intent.putExtra("map", hashMap);
startActivity(intent);
然后在接收Activity中:
And then in the receiving Activity:
protected void onCreate(Bundle bundle) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
HashMap<String, String> hashMap = (HashMap<String, String>)intent.getSerializableExtra("map");
Log.v("HashMapTest", hashMap.get("key"));
}
这篇关于如何使用意图将哈希图值发送到另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!