I am trying to pass an ArrayList<HashMap<String, String>> between Activities. How would I do this with Intents?
            Asked
            
        
        
            Active
            
        
            Viewed 3,150 times
        
    1
            
            
         
    
    
        Brian Roach
        
- 76,169
- 12
- 136
- 161
 
    
    
        Johnathan Au
        
- 5,244
- 18
- 70
- 128
- 
                    1Since all of those implement `Serianlizable`, I think you can through `Intent.putExtra()`. ([Source](http://developer.android.com/reference/android/content/Intent.html#putExtra%28java.lang.String,%20java.io.Serializable%29)) – Mike D Mar 07 '13 at 00:16
2 Answers
2
            ArrayList are Serializable (as well as HashMaps and Strings), so try putExtra(String, Serializable).  
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
Intent intent = new Intent(this, MyActivity.class); 
intent.putExtra("key", list);
 
    
    
        Sam
        
- 86,580
- 20
- 181
- 179
- 
                    Sorry, I don't understand. Do you mean putExtra("list", (Serializable) myList) ? – Johnathan Au Mar 07 '13 at 00:15
- 
                    Yes, you need to cast it to a Serializable if you use `ListmyList = new ArrayList – Sam Mar 07 '13 at 00:19();` since a List is an interface and the actual class doesn't _necessarily_ implement Serializable. 
- 
                    Hmm I did the above and I'm having trouble getting the data in my other activity. I am doing this: Intent intent = getIntent(); Bundle bundle = intent.getExtras(); ArrayList> list = bundle.get("list"); – Johnathan Au Mar 07 '13 at 00:21
- 
                    Try: [`bundle.getSerializable("list")`](https://developer.android.com/reference/android/os/Bundle.html#getSerializable%28java.lang.String%29) – Sam Mar 07 '13 at 00:30
- 
                    Thanks it works but I get this: Type safety: Unchecked cast from Serializable to ArrayList>. Should I worry about that? – Johnathan Au Mar 07 '13 at 00:36
- 
                    1Since you put the Serializable data in yourself, you can ignore it and safely suppress this warning. Here is more detail on why it's happening: [How do I address unchecked cast warnings?](http://stackoverflow.com/q/509076/1267661) – Sam Mar 07 '13 at 00:46
0
            
            
        Use putExtra(String, Serializable) to pass the value in an Intent and getSerializableExtra(String) method to retrieve the data.
Passing an ArrayList> 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);
 
    
    
        Zar E Ahmer
        
- 33,936
- 20
- 234
- 300