如何列出listview中的所有值

时间:2015-10-07 14:42:23

标签: android listview

我有一组值要返回到列表视图,但现在我只能逐个返回。

如何更改代码以便能够显示整个数组列表?

Listview代码

 listView = (ListView) findViewById(R.id.loan_updates);

    String[] values = new String[]{"Device:"+ device, "Period:" + period};

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, values);

    listView.setAdapter(adapter);

我的数组将返回到listview中填充

 Bundle bundle=intent.getExtras();
        ArrayList<Loans> loans=(ArrayList<Loans>)bundle.getSerializable("key");
    for (int i = 0; i < loan.size(); i++) {
       String device=loans.get(i).getdevice();
       String period=loans.get(i).getperiod();
    }

1 个答案:

答案 0 :(得分:0)

您可以将ArrayList<Loans>转换为String[],如下所示:

ArrayList<Loans> loans=(ArrayList<Loans>)bundle.getSerializable("key");
String[] values = new String[loans.size()];

for (int i = 0; i < loan.size(); i++) {
   String device=loans.get(i).getdevice();
   String period=loans.get(i).getperiod();
   values[i] = "Device:"+ device + "Period:" + period;
}

之后,您可以像现在一样创建适配器并将其设置为listview:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, android.R.id.text1, values);

listView.setAdapter(adapter);