How to Send Hashmap Value to Another Activity Using an Intent

How to send hashmap value to another activity using an intent

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

passing hash map to an intent

use this

   intent.putExtra("hash",data.toString());

OR

   intent.putSerializableExtra("hash",data)

passing a HashMap from one activity to another, android

Use intent.putExtra(String, Serializable) - see http://developer.android.com/reference/android/content/Intent.html#putExtra%28java.lang.String,%20java.io.Serializable%29.
i.e.

intent.putExtra("placesHashMap", places)

In the receiving activity use

HashMap<String, String> places = (HashMap<String, String>) intent.getSerializableExtra("placesHashMap");

How to send MapString, ListString through a intent

You will have to use Serializable and pass the map through intent.
Code Example of Data Sender is as follows :

Map map = new HashMap<String,List<String>>();

List<String> l1 = new ArrayList();

l1.add("HEllo");
l1.add("John");
l1.add("Michael");
l1.add("Jessy");

map.put("Names" , l1);

Intent intent = new Intent("CurrentActivityName".this, "DestinationActivityName".class);

intent.putExtra("Map",(Serializable) map);

startActivity(intent);

Code for Receiver:

Map map = new HashMap<String,List>();
map = (Map) getIntent().getSerializableExtra("Map");

Now you can access the data using variable named map.

Android - How to pass HashMapString,String between activities?

This is pretty simple, All Collections objects implement Serializable (sp?) interface which means they can be passed as Extras inside Intent

Use putExtra(String key, Serializable obj) to insert the HashMap and on the other Activity use getIntent().getSerializableExtra(String key), You will need to Cast the return value as a HashMap though.

pass hashmap data from an activity

Use intent.putExtra(String, Serializable) - see

intent.putExtra("hashMap", fileList);

In the receiving activity use

 HashMap<Integer, boolean[]> fileList = (Integer, boolean[]) intent.getSerializableExtra("hashMap");

And also chnage the below code

Intent intent = new Intent(ExpListViewAdapterWithCheckbox.this, MainActivity.class);

To

Intent intent = new Intent(context, MainActivity.class);

Unable to send a Hash Map to a new Activity through putExtra

The problem is that your class com.example.chalaka.myapplication.Point does not implement Parcelable nor Serializable.

Do so and the code above will work as you expect.



Related Topics



Leave a reply



Submit