如何使用意图将hashmap值发送到其他活动 [英] How to send hashmap value to another activity using an intent

查看:88
本文介绍了如何使用意图将hashmap值发送到其他活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 HashMap 值从一个Intent发送到第二个Intent?



另外,如何在第二个Activity中检索 HashMap 值?


< Java的HashMap类扩展了 Serializable 接口,这可以很容易地将它添加到一个intent中,使用 意图.putExtra(String,Serializable) 方法。



在接收意图的活动/服务/请拨打
Intent.getSerializableExtra (字符串) 与您使用putExtra的名称。

例如,发送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中:

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


How to send HashMap value from one Intent to second Intent?

Also, how to retrieve that HashMap value in the second Activity?

解决方案

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.

In the activity/service/broadcast receiver that receives the intent, you then call Intent.getSerializableExtra(String) with the name that you used with putExtra.

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

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

这篇关于如何使用意图将hashmap值发送到其他活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆