Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
循环ArrayList以在第二个活动Java中的多行ListView中显示?_Java_Android_Listview_Arraylist - Fatal编程技术网

循环ArrayList以在第二个活动Java中的多行ListView中显示?

循环ArrayList以在第二个活动Java中的多行ListView中显示?,java,android,listview,arraylist,Java,Android,Listview,Arraylist,我创建了第二个活动来显示ArrayList中的所有元素。例如,如果ArrayList在MainActivity中包含以下内容: //This is an Object type thingsList = ["This is list1","This is list2"]; Intent intent = new Intent(this, Activity2.class); Bundle b = new Bundle; b.putString("Lists", thingsList.toStr

我创建了第二个活动来显示ArrayList中的所有元素。例如,如果ArrayList在MainActivity中包含以下内容:

//This is an Object type
thingsList = ["This is list1","This is list2"]; 

Intent intent = new Intent(this, Activity2.class);
Bundle b = new Bundle;
b.putString("Lists", thingsList.toString());
intent.putExtras(b);
startActivity(intent);
我在Activity2.java中有这个

ListView newListView = (ListView)findViewById(R.id.newListView);
Bundle b = getIntent().getExtras();
String newList = b.getString("Lists");
ArrayAdapter adapterList = new ArrayAdapter(this, R.layout.list_label, Collections.singletonList(newList));
newListView.setAdapter(adapterList);
它现在所做的是:

[This is list1, This is list2]
如何循环遍历arraylist并使其显示在不同的行上

This is list1
This is list2
我试过这样做,但没有成功

thingsList = ["This is list 1","This is list 2"];

Intent intent = new Intent(this, Activity2.class);
Bundle b = new Bundle;
for (Object makeList: thingsList) {
    b.putString("Lists", makeList.toString());
    intent.putExtras(b);
}
startActivity(intent);
它只抓取arraylist中的最后一个元素,就像

This is list2

请提前感谢,我不确定这个问题是否有意义。

您需要使用
Bundle.putStringArrayList()
而不是
putString()
。而且,当使用
Intent
s时,您实际上可以跳过
捆绑包
步骤,直接向Intent添加额外内容(Android将在幕后为您创建并填充捆绑包)

我不知道您是如何获得
thingsList
引用的,但如果它只是一个通用的
列表
最安全的做法是在将其添加到捆绑包之前构建一个新的
ArrayList

thingsList = ["This is list1","This is list2"]; 

// you can skip this if thingsList is already an ArrayList
ArrayList<String> thingsArrayList = new ArrayList<>(thingsList);

Intent intent = new Intent(this, Activity2.class);
intent.putStringArrayListExtra("Lists", thingsArrayList); // or use thingsList directly
startActivity(intent);
是的,适当的方法是由@Ben p建议的

我只是指出你尝试过的方法中的错误

thingsList = ["This is list 1","This is list 2"];

Intent intent = new Intent(this, Activity2.class);
Bundle b = new Bundle;
for (Object makeList: thingsList) {
    b.putString("Lists", makeList.toString());
    intent.putExtras(b);
}
startActivity(intent);
由于Bundle对象是在for循环外部创建的,因此最后一个值将被前一个值覆盖

您应该按如下所示每次创建新对象

 for (Object makeList: thingsList) {
        Bundle b=new Bundle();
        b.putString("Lists", makeList.toString());
        intent.putExtras(b);
    }
我不知道有什么“列表”方法可以做到这一点。让它变得更容易。这个答案非常有用。
 for (Object makeList: thingsList) {
        Bundle b=new Bundle();
        b.putString("Lists", makeList.toString());
        intent.putExtras(b);
    }