设为首页收藏本站

Scripts 学盟

 找回密码
 加入学盟

QQ登录

只需一步,快速开始

查看: 2405|回复: 7
打印 上一主题 下一主题

android(五) ListView [复制链接]

Rank: 8Rank: 8

跳转到指定楼层
1#
那个谁 发表于 2011-5-17 14:44:08 |只看该作者 |倒序浏览
一个ListView显示出来需要3个东西:
1,listview(用来显示数据的列表)。
2,Data(需要显示的数据)。
3,一个绑定Data和Listview的适配器ListAdapter。
一,ListView
1,ListView的每一项其实都是TextView。
2,通过setAdapter方法来调用一个listAdapter来绑定数据。
二,ListAdapter
1,ListAdapter是绑定Data和Listview的适配器。但是,它是接口,需要使用它的子类。
常见的子类有:arrayAdapter,SimpleAdapter ,CursorAdapter
2,android系统默认提供了很多默认的布局方式,也可以自定义。

Android.R.layout.simple_list_item_1:每一项只有一个TextView
Android.R.layout.simple_list_item_2:每一项有两个TextView
Android.R.layout.simpte.list_item_single_choice,每一项有一个TextView,但是这一项可以被选中。
三,ArrayAdapter
1,数组适配器,它的作用就是一个数组和listview之间的桥梁,它可以将数组里边定义的数据一一对应的显示在Listview里边。
2,ListView的每个TextView里边显示的内容就是数组里边的对象调用toString()方法后生成的字符串。
3,这是一个简单创建一个list的例子其中的代码:
  1. ListView listview=new ListView(this);
  2. // 构造一个listview对象
  3. String[] data = {“google”,”amazon”,”facebook”};
  4. // 构造一个数组对象,也就是数据
  5. listview.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_single_choice, data));
  6. //构造一个array适配器,然后listview对象通过setAdapter方法调用适配器来和自己绑定数据
  7. list.setItemsCanFocus(true);
  8. list.setChoiceMode(listview.CHOICE_MODE_MULTIPLE);
  9. setContentView(listview);
复制代码
四,SimpleAdapter
1,作用是ArrayList和ListView的桥梁。这个ArrayList里边的每一项都是一个Map<String,?>类型。ArrayList当中的每一项Map对象都和ListView里边的每一项进行数据绑定一一对应。
2,SimpleAdapter的构造函数:
SimpleAdapter(Context  context, List<? extends Map<String, ?>>  data, int resource, String[]  from, int[] to)
参数:
1,context:上下文。
2,data:基于Map的list。Data里边的每一项都和ListView里边的每一项对应。Data里边的每一项都是一个Map类型,这个Map类里边包含了ListView每一行需要的数据。
3,resource :就是一个布局layout,可引用系统提供的,也可以自定义。
4,from:这是个名字数组,每个名字是为了在ArrayList数组的每一个item索引Map<String,Object>的Object用的。
5,to:里面是一个TextView数组。这些TextView是以id的形式来表示的。例如:Android.R.id.text1,这个text1在layout当中是可以索引的。
3,通过一个类子来理解下:
listitem.xml文件:
  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <LinearLayout xmlns:android=”[url]http://schemas.android.com/apk/res/android[/url]”
  3. android:orientation=”horizontal” android:layout_width=”fill_parent”
  4. android:layout_height=”wrap_content”>
  5. <TextView android:id=”@+id/mview1″ android:layout_width=”100px”
  6. android:layout_height=”wrap_content” />
  7. <TextView android:id=”@+id/mview2″
  8. android:layout_width=”wrap_content”
  9. android:layout_height=”wrap_content” />
  10. <TextView android:id=”@+id/mview3″
  11. android:layout_width=”wrap_content”
  12. android:layout_height=”wrap_content” />
  13. </LinearLayout>
复制代码
下面是activity文件的部分代码
  1. List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
  2. Map<String,Object> item;
  3. item = new HashMap<String,Object>();
  4. item.put(“姓名”,”漠漠”);
  5. item.put(“性别”,”非男”);
  6. item.put("描述","大坏蛋、大坏蛋、大坏蛋、大坏蛋");
  7. data.add(item);
  8. item = new HashMap<String,Object>();
  9. item.put(“姓名”,”漠漠”);
  10. item.put(“性别”,”女”);
  11. item.put("描述","大坏蛋、大坏蛋、大坏蛋、大坏蛋");
  12. data.add(item);
  13. // 上面是构造数据部分。
  14. ListView listview= new ListView(this);
  15. //构造listview对象。
  16. SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.listitem,new String[]{“姓名”,”性别”,"描述"},new int[]{R.id.TextView01,R.id.TextView02,R.id.TextView03});
  17. /*构造一个适配器。
  18. *    1,第三个参数是说明用的是自定义的布局R.layout.listtiem。
  19. * 2,第四和第五个参数一起理解:
  20. *          把我们添加数据时姓名那一列对应到R.id.TextView01这个TextView中,把性别对应到R.id.TextView02这个TextView中。
  21. *          如果把from改为new String[]{“姓名”,”描述”},测试下就会明白。
  22. */
  23. listview.setAdapter(adapter);
  24. setContentView(listview);
复制代码
结果是:
漠漠 非男  大坏蛋、大坏蛋、大坏蛋、大坏蛋
漠漠 女  大坏蛋、大坏蛋、大坏蛋、大坏蛋
1

查看全部评分

分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
分享分享0 收藏收藏0

管理员

超级大菜鸟

Rank: 9Rank: 9Rank: 9

2#
Alvin 实名认证  发表于 2011-5-17 16:48:29 |只看该作者
不能这样的。。。

使用道具 举报

管理员

超级大菜鸟

Rank: 9Rank: 9Rank: 9

3#
Alvin 实名认证  发表于 2011-5-18 09:20:26 |只看该作者


这么快就补上来了... 给力

使用道具 举报

Rank: 8Rank: 8

4#
momo 发表于 2011-5-18 09:33:38 |只看该作者
杰杰,你。。。。。。。。。。。。。。。
过了爱做梦的年纪
轰轰烈烈不如平静

使用道具 举报

Rank: 8Rank: 8

5#
那个谁 发表于 2011-5-18 09:38:39 |只看该作者
回复 4# momo


    漠漠姐有问题吗?{:3_42:}

使用道具 举报

Rank: 8Rank: 8

6#
momo 发表于 2011-5-18 09:41:22 |只看该作者
回复 5# 那个谁


    过来,我告诉你,哼哼哼~~~~~~
过了爱做梦的年纪
轰轰烈烈不如平静

使用道具 举报

Rank: 8Rank: 8

7#
那个谁 发表于 2011-5-18 12:01:17 |只看该作者
回复 6# momo


    我不在这吗、{:3_46:}

使用道具 举报

Rank: 8Rank: 8

8#
momo 发表于 2011-5-18 13:02:09 |只看该作者
本帖最后由 momo 于 2011-5-18 13:03 编辑

22.gif 哼哼,过来,挠死你
过了爱做梦的年纪
轰轰烈烈不如平静

使用道具 举报

您需要登录后才可以回帖 登录 | 加入学盟

手机版|Scripts 学盟   |

GMT+8, 2024-4-30 12:19 , Processed in 1.125301 second(s), 17 queries .

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部