Android中ListView的简单使用

动态添加单行列表:

首先前提是你的布局文件里有一个ListView

单行列表的添加只需要一个list集合即可,使用ArrayAdapter数组适配器绑定更新就行了
首先声明一个ArrayAdapter对象:
ArrayAdapter adapter adapter = new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, List);
参数1:上下文对象,一般来说是this
参数2:列表显示方式,android.R.layout.simple_expandable_list_item_1 为常量,表示单行列表
参数3:集合,可以使用数组。
最后使用listView.setAdapter(adapter);方法即可显示列表

adapter = new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, new String[]{"列表1","列表2","列表3"});
listView.setAdapter(adapter);
这一句代码添加到按钮事件里面即可使用;

也可以使用集合的方式,代码如下:

List<String> List = new ArrayList<String>();//声明集合
List.add("列表1");
List.add("列表2");
List.add("列表3");

adapter = new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, List);
listView.setAdapter(adapter);

猜你喜欢

转载自www.cnblogs.com/zhanghongxian666/p/9971220.html