Android 的 ArrayAdapter - 添加字符串列表视图而不是替换 [英] Android's ArrayAdapter - add strings listview instead of replace

查看:21
本文介绍了Android 的 ArrayAdapter - 添加字符串列表视图而不是替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我向列表视图添加数组的方式:

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?

推荐答案

您需要使用 ArrayListmy_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.

这篇关于Android 的 ArrayAdapter - 添加字符串列表视图而不是替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆