ListView -- MarsChen Android 开发教程学习笔记


ListView 的基本使用
1、创建Activity 继承自ListActivity,两个布局文件。
一个是常用的main.xml ,线性布局,嵌入listView ,用Android 内置ID:“android:list”,并且包含两个命令行。
另一个是控制list 的布局文件user.xml,在一个线性布局文件中嵌套两个textView 。线性布局是横向排列,两个textView  代表每一栏中横向排列两个字符。
2、Activity 文件中创建HashMap 对象,代表每栏中的数据,每个HashMap 内放两个键值对。调用put 方法放置两个键值对。
  
  
HashMap<String, String> map1 = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
HashMap<String, String> map3 = new HashMap<String, String>();
map1.put("键" , "值");
map1.put("键" , "值");
map2.put("键" , "值");
map2.put("键" , "值");
map3.put("键" , "值");
map3.put("键" , "值");

3、Activity 文件中创建ArrayList<HashMap<String, String>> 的list 对象,调用add 方法将HashMap 对象加入列表形成完整的ListView 形态。
  
  
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>
list.add(map1);
list.add(map2);
list.add(map3);


4、还要创建一个SimpleAdapter 对象listAdapter ,第一个参数代表Activity ,第二个即使ArrayList 对象,第三个参数代表放置表格内容的XML 文件,即上文中的user.xml ,第 四个参数是字符数组,列表有几列数组就有几个参数数组中的参数放置的是键值对中的键,第五个参数是一个int 数组,放置两个R.id,对应user.xml 文件中的两个textView 。

SimpleAdapter listAdapter = new SimpleAdapter(this ,list ,R.layout.user ,new String[]{"user_name","user_ip"} ,new int[]{R.id.user_name,R.id.user_ip} );


5、最后要设置适配器

setListAdapter(listAdapter);



ExpandableListActivity 和SimpleExpandableListAdapter 的基本使用
ExpandableListeActivity 是列表组。

1、创建一个Activity 继承ExpandableListActivity 。
分别创建两种Map和List 成对的对象,一种是一级条目的,一种是二级条目的。创建和使用方法与listView 相同,当然还要用put 和 add 方法。
  
  
List<Map<String, String>> groups = new ArrayList<Map<String, Stirng>>();
Map<String, String> group = new HashMap<String,String>>();
List<Map<String, String>> child1 = new ArrayList<Map<String, Stirng>>();
Map<String, String> child1data1= new HashMap<String,String>>();


定义一个list 对象,存放所有二级条目。
  
  
List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
childs.add(child1);
childs.add(child2);


2、除了原先的默认XML 文件之外还要创建两个布局文件,分别代表组和组值。
默认XML 文件中添加ExpandableListView 的标签,ID直接引用Android 设定好的“android:list”,并在标签内添加android:drawSelectorOnTop="false",代表被选择的时候颜 色是否覆盖所选列表。
第一个和第二个XML文件仍和listView 一样放置TextView ,两者区别不过是字体大小不同。

3、生成SimpleExpandableListAadapter 对象,第一个参数是Activity,第二个参数是一级条目的数据,第三个参数用来设置一级条目样式的布局文件,第四个参数指定一级条目

数据的键,第五个参数指定一级条目的id,第六个参数指定二级条目样式的布局文件,第七个参数指定二级条目的键,第八个指定二级条目的id 。

SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(this, groups, R.layout.group, new String[]{"group"}, new int[] {R.id.group}, childs , R.layout.child , new String[]{"child"},new int[]{R.id,child});

4、

setListAdapter(sela);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值