开发者

How to pass ArrayList using putStringArrayListExtra()

开发者 https://www.devze.com 2023-01-21 23:09 出处:网络
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

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?

开发者_如何学JAVA
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); 


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


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消