Java 如何使用 putStringArrayListExtra() 传递 ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4030115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:41:55  来源:igfitidea点击:

How to pass ArrayList using putStringArrayListExtra()

javaandroidarraylist

提问by Miya

Hi I want to pass an Arraylistfrom one activityto 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?

嗨,我想Arraylist从一个传递activity到另一个。我使用putStringArrayListExtra(),但显示错误:"The method putStringArrayListExtra(String,ArrayList is undefined for the type bundle."是否还有其他方法可供传递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); 

采纳答案by Maneesh

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

尝试下面的一个将一维数组传递给额外的 Arraylist

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

回答by Maneesh

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

您必须定义 String 类型的 ArrayList。您不能在 putStringArrayListExtra 中传递通用 ArrayList。下面是正确的代码。

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


Now access this ArrayList in other activity like this.

现在在像这样的其他活动中访问这个 ArrayList。

ArrayList<String> cl= new ArrayList<String>();
cl =getIntent().getExtras().getStringArrayList("lists");

回答by Maneesh

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

回答by user2227186

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