Android的ArrayAdapter - 添加字符串列表视图,而不是取代视图、字符串、而不是、列表

2023-09-05 01:28:18 作者:君子坦荡荡

这是我如何添加一个数组列表视图:

This is how I add an array to listview:

ListView my_listview = (ListView)findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, my_array);
my_listview.setAdapter(adapter);

my_array是文件的URI。

my_array is uri of a file.

它运作良好。但接下来的下一次我要数组新数组,然后我不想更换成新的,而不是添加新的以现有的。我怎样才能实现这个目标?

It works well. But next next time I want to array a new array, then I don't want to replace with with new one instead add new ones to the existing ones. How can I accomplish that?

推荐答案

您需要使用一个的ArrayList&LT;字符串&GT; my_array ,而不是的String [] my_array ,然后你可以这样做:

You need to be using an ArrayList<String> my_array rather than a String[] my_array and then you can do:

my_array.addAll(other_array);

从一个新的阵列添加至阵列元素。然后,你可以这样做:

to add elements from a new array to your array. Then you can do:

adapter.notifyDataSetChanged();

与新元素更新您的ListView。

to update your ListView with the new elements.