Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass ArrayList<HashMap<String, String>>from one activity to another

Tags:

android

How i can pass Array List from one Activity to another my array list is shown as follows

ArrayList<HashMap<String, String>>
like image 672
sandy Avatar asked Jun 15 '11 09:06

sandy


People also ask

How do you pass a HashMap from one activity to another?

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. Show activity on this post.

Which is faster HashMap or ArrayList?

The ArrayList has O(n) performance for every search, so for n searches its performance is O(n^2). The HashMap has O(1) performance for every search (on average), so for n searches its performance will be O(n). While the HashMap will be slower at first and take more memory, it will be faster for large values of n.

Can you make an ArrayList of Hashmaps?

A HashMap contains key-value pairs, there are three ways to convert a HashMap to an ArrayList: Converting the HashMap keys into an ArrayList. Converting the HashMap values into an ArrayList. Converting the HashMap key-value pairs into an ArrayList.


3 Answers

Use putExtra(String, Serializable) to pass the value in an Intent and getSerializableExtra(String) method to retrieve the data.

Passing an ArrayList<HashMap<String, String>> from Activity A to Activity B

Intent intent = new Intent(this, B.class);
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("sunil", "sahoo");
ArrayList<HashMap<String, String>> arl = new ArrayList<HashMap<String, String>>();
arl.add(hm);
intent.putExtra("arraylist", arl);
startActivityForResult(intent, 500);

Retrieve the data in Activity B

ArrayList<HashMap<String, String>> arl = (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("arraylist");
System.out.println("...serialized data.."+arl);
like image 86
Sunil Kumar Sahoo Avatar answered Sep 17 '22 19:09

Sunil Kumar Sahoo


You can use a Bundle to pass elements from one Activity to another.

Check this out: http://developer.android.com/reference/android/os/Bundle.html

You create the Bundle, put it into the Intent, and then on the new activity, you get it and extract the elements you need.

It goes like this:

Bundle b = new Bundle();
String s = "hello";
b.putString("example", s);
intent.putExtras(b);

and then on the new activity:

Bundle b = this.getIntent().getExtras(); 
String s = b.getString("example");
like image 41
seth Avatar answered Sep 20 '22 19:09

seth


here is another technique, I used following line to define ArrayList in firstClass.

static ArrayList al=new ArrayList();

In second activity, i used following line to get the data of ArrayList from firstClass,

firstClass.al.size();
like image 21
user609239 Avatar answered Sep 16 '22 19:09

user609239