【问题标题】:Loop through ArrayList to display in multiline ListView in second Activity Java?循环 ArrayList 以在第二个 Activity Java 中的多行 ListView 中显示?
【发布时间】:2019-02-27 12:56:34
【问题描述】:

我创建了第二个 Activity 来显示 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]

如何循环遍历数组列表并使其显示在不同的行上

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

它所做的只是抓取数组列表中的最后一个元素,例如

This is list2

提前致谢,不知道这个问题是否有意义。

【问题讨论】:

    标签: java android listview arraylist


    【解决方案1】:

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

    我不确切知道您是如何获得thingsList 引用的,但如果它只是一个通用的List,最安全的做法是在将其添加到捆绑包之前构建一个新的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);
    

    然后,另一方面,您需要将其作为List 获取。同样,您可以跳过Bundle,直接访问Intent 附加功能:

    ListView newListView = (ListView)findViewById(R.id.newListView);
    List<String> newList = getIntent().getStringArrayListExtra("Lists");
    ArrayAdapter adapterList = new ArrayAdapter(this, R.layout.list_label, newList);
    newListView.setAdapter(adapterList);
    

    【讨论】:

    • 我不知道有“列表”方法可以做到这一点。让它变得更容易。这个答案非常有帮助。
    【解决方案2】:

    是的,@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);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-13
      • 2018-04-05
      • 1970-01-01
      • 2019-10-20
      • 1970-01-01
      • 1970-01-01
      • 2020-06-15
      • 2017-06-22
      相关资源
      最近更新 更多