views:

22

answers:

3

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 al=new ArrayList();
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); 
A: 

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
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
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
A: 

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
Thank you maneesh.. Can you please tell me how to pass a multidimensional array to another activity using serialization or using ArrayList itself...
Miya
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
A: 

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