15

Hi I want to pass an Arraylist from one activity to another. I use putStringArrayListExtra(), but there shows an error : "The method putStringArrayListExtra(String,ArrayList is undefined for the type bundle." Is there any other method available for passing ArrayList?

String test[]=new String[3]; 
ArrayList<String[]> al=new ArrayList<String[]>();  
int x,y;
test[0]="1";  
test[1]="2";  
test[2]="3";  
al.add(test);  

test = new String[3]; 
test[0]="4";  
test[1]="5";  
test[2]="6";  
al.add(test);  

Bundle list_bundle=new Bundle(); 
list_bundle.putStringArrayListExtra("lists",al); 
Intent list_intent= new Intent(v.getContext(), view_all_selected.class); 
list_intent.putExtras(list_bundle); 
startActivityForResult(list_intent, 2); 
buræquete
  • 14,226
  • 4
  • 44
  • 89
Miya
  • 203
  • 3
  • 9
  • 16

4 Answers4

20

Try this It worked for me

1st Activity

ArrayList<String> ar=new ArrayList<String>();
ar.add("Apple");
ar.add("Banana");

Intent i=new Intent(this,Route.class);
i.putStringArrayListExtra("list", ar);
startActivity(i);

2nd Activity

ArrayList<String> ar1=getIntent().getExtras().getStringArrayList("list");   
user2227186
  • 221
  • 3
  • 4
13

try below one to pass 1-D array to Arraylist in extras

ArrayList<String> al = new ArrayList<String>();
String arr[] = {"Zero", "One", "Two"}; 
//Convert string array to a collection 
Collection l = Arrays.asList(arr);
al.addAll(l); 
i.putStringArrayListExtra("ar", al);
Maneesh
  • 6,098
  • 5
  • 36
  • 55
  • Thank you maneesh.. Can you please tell me how to pass a multidimensional array to another activity using serialization or using ArrayList itself... – Miya Oct 27 '10 at 09:05
  • You can convert 2-d Array to 1-D ArrayList/1-D Array and send via extras and in other activity reconvert into 2-d array. Posted a solution . Please check the same. – Maneesh Oct 27 '10 at 10:12
3

First Activity :

 ArrayList<String> al = new ArrayList<String>();
    int ROWS = 2;
                        int COLS = 1;
                        String[][] a2 = new String[ROWS][COLS];
                        a2[0][0]="one";
                        a2[1][0]="two";
                        for(int i=0;i<ROWS;i++)
                        {
                            for(int j=0;j<COLS;j++)
                            {
                                al.add(a2[i][j]);
                            }
                        }
     i.putStringArrayListExtra("ar", al);
                        i.putExtra("ROWS", ROWS);
                        i.putExtra("COLS", COLS);

Second Activity :

ArrayList<String> test = new ArrayList<String>();
test=getIntent().getExtras().getStringArrayList("ar");
        int ROWS=getIntent().getExtras().getInt("ROWS");
        int COLS=getIntent().getExtras().getInt("COLS");
        String[][] a2 = new String[ROWS][COLS];
        int index=0;
        for(int i=0;i<ROWS;i++)
        {
            for(int j=0;j<COLS;j++)
            {
                a2[i][j]=test.get(index++);
            }
        }
Maneesh
  • 6,098
  • 5
  • 36
  • 55
1

You have to define ArrayList of type String. you can't pass Generic ArrayList in putStringArrayListExtra. Below is the correct code.

-----
ArrayList<String> al = new ArrayList<String>();
------
-------
 list_bundle.putStringArrayListExtra("lists",al); 
------

Now access this ArrayList in other activity like this.

ArrayList<String> cl= new ArrayList<String>();
cl =getIntent().getExtras().getStringArrayList("lists");
Maneesh
  • 6,098
  • 5
  • 36
  • 55
  • maneesh, i used the solution that you gave to me. But the problem is still remaining there.That is I have to pass a 1-Dimensional array to this ArrayList. Please tell me how to pass this one-dimensional array to list or to pass a 2-Dimensional array to another activity.. – Miya Oct 27 '10 at 05:54
  • try below one to pass 1-D array to Arraylist in extras ArrayList al = new ArrayList();String arr[] = {"Zero", "One", "Two"}; //Convert string array to a collection Collection l = Arrays.asList(arr);al.addAll(l); i.putStringArrayListExtra("ar", al); – Maneesh Oct 27 '10 at 06:36