代码改变世界

Android常用控件 -- ListView

2011-11-15 11:11  刘XX  阅读(311)  评论(0)    收藏  举报

 

三大元素

1.ListView  -- 用来显示数据的试图
2.适配器     -- 用来把数据映射到ListView上的中介
3.数据        -- 具体的将被映射的元素

 

ListView:

具体实现:
1.布局文件添加ListView控件;   

布局文件
 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical" android:layout_width="fill_parent"
4 android:layout_height="fill_parent">
5
6 <ListView
7 android:id="@+id/lv"
8 android:layout_width="wrap_content"
9 android:layout_height="match_parent">
10 </ListView>
11
12 </LinearLayout>

  通过findViewById获得

ListView lv = (ListView) findViewById(R.id.lv);


 

2.继承ListActivity,通过getListView()获得  

View Code
1 public class MainActivity extends ListActivity {
2 @Override
3 public void onCreate(Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
5 ListView lv = getListView();
6
7 }


   

适配器:

主要分为三种    
ArrayAdapter -- 最简单,只能显示一样文字
SimpleAdapter -- 有最好的扩充性,可以自定义各种效果
SimpleCursorAdapter -- 对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来

ArrayAdapter:

ArrayAdapter需要三个参数:Context,布局文件,数据源。方法如下:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Context context, int textViewResourceId, List<T> objects);

例子:

View Code
 1 public class MainActivity extends ListActivity {
2 @Override
3 public void onCreate(Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
5 ListView lv = getListView();
6 ArrayAdapter<String> adapter;
7 adapter = new ArrayAdapter<String>(this,
8 android.R.layout.simple_expandable_list_item_1,
9 new String[]{"aaa","bbb","ccc"}
10 );
11 lv.setAdapter(adapter);
12 }
13 }

 

    

SimpleAdapter:

simpleAdapter的扩展性最好,可以自定义每项的布局,可以放入各种控件

首先我们要自定义一个用来显示每一项内容的布局文件:(我这里比较简单就放了一个TextView和Button)

布局文件
 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="horizontal"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent">
6
7 <TextView
8 android:text="TextView"
9 android:id="@+id/txt"
10 android:layout_width="wrap_content"
11 android:layout_height="wrap_content">
12 </TextView>
13 <Button
14 android:text="Button"
15 android:id="@+id/btn"
16 android:layout_width="wrap_content"
17 android:layout_height="wrap_content">
18 </Button>
19
20
21 </LinearLayout>



simpleAdapter的数据一般都是HashMap构成的List,list的每一节对应ListView的每一行。HashMap的每个键值数据映射到布局文件中对应id的组件上。

Java代码
 1 public class MainActivity extends ListActivity {
2 @Override
3 public void onCreate(Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
5 ListView lv = getListView();
6 SimpleAdapter adapter;
7 adapter = new SimpleAdapter(this, //Context
8 getData(), //数据
9 R.layout.main, //布局文件
10 new String[]{"txt","btn"}, //HashMap的Key
11 new int[]{R.id.txt,R.id.btn}); //布局文件对应的ID
12 lv.setAdapter(adapter);
13 }
14
15 //数据
16 private List<Map<String, Object>> getData() {
17 List<Map<String, Object>> ls = new ArrayList<Map<String,Object>>();
18
19 Map<String, Object> map = new HashMap<String, Object>();
20 map.put("txt", "txt_01");
21 map.put("btn", "btn_01");
22 ls.add(map);
23
24 map = new HashMap<String, Object>();
25 map.put("txt", "txt_02");
26 map.put("btn", "btn_02");
27 ls.add(map);
28
29 map = new HashMap<String, Object>();
30 map.put("txt", "txt_03");
31 map.put("btn", "btn_03");
32 ls.add(map);
33 return ls;
34 }
35 }



布局文件的各组件分别映射到HashMap的各元素上,完成适配。