vListView是手机系统中使用非常广泛的一种组件,它以垂直列表的形式显示所有列表项,创建ListView两种方式。

直接使用 ListView 创建。
Activity 继承 ListActivity
 
XML 属性
说明
android:choiceMode
设置 ListView 的选择行为
android:divider
设置 List 列表项的分隔条 ( 可用颜色或 Drawable)
android:dividerHeight
设置分隔条的高度
android:entries
指定一个数组资源,根据该资源生成 ListView
android:footerDividersEnabled
值为 false 时不在 footerView 之前绘制分隔条
android:headerDividersEnabled
值为 false 时不在 headerView 之后绘制分隔条
 

根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter:


ArrayAdapter最为简单,只能展示一行字。
 SimpleAdapter有最好的扩充性,可以自定义出各种效果。
 SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来。


 simpleAdapter
 的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。
使用simpleAdapter的数据用一般都是HashMap构成的List,list的每一节对应ListView的每一行。
HashMap的每个键值数据映射到布局文件中对应id的组件上。因为系统没有对应的布局文件可用,我们可以自己定义一个布局*.xml。
有时候,列表不光会用来做显示用,我们同样可以在在上面添加按钮。但是按钮是无法映射的,
即使你成功的用布局文件显示出了按钮也无法添加按钮的响应,这时就要研究一下ListView是如何现实的了,而且必须要重写一个类继承BaseAdapter。


SimpleCursorAdapter
简单的说就是方便把从游标得到的数据进行列表显示,并可以把指定的列映射到对应的TextView中。
startManagingCursor(cursor);我们将获得的Cursor对象交由Activity管理,这样Cursor的生命周期和Activity便能够自动同步,省去自己手动管理Cursor。

布局文件:

 
  
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:orientation="vertical" 
  4. android:layout_width="fill_parent" 
  5. android:layout_height="fill_parent" 
  6. > 
  7.         <ListView   
  8.             android:id="@+id/list2" 
  9.             android:layout_width="fill_parent" 
  10.             android:layout_height="wrap_content" 
  11.             android:divider="@drawable/line"> 
  12.         </ListView> 
  13.         <TextView   
  14.             android:layout_width="fill_parent" 
  15.             android:layout_height="2px" 
  16.             android:background="#ff0000" 
  17.             /> 
  18.         <ListView   
  19.             android:id="@+id/list3" 
  20.             android:layout_width="fill_parent" 
  21.             android:layout_height="wrap_content" 
  22.             android:divider="@drawable/line"> 
  23.         </ListView> 
  24.           
  25. </LinearLayout> 

列表布局文件:

 
  
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:orientation="vertical" 
  4. android:layout_width="fill_parent" 
  5. android:layout_height="fill_parent" 
  6. > 
  7.  
  8.         <TextView   
  9.             android:id="@+id/list_name" 
  10.             android:layout_width="fill_parent" 
  11.             android:layout_height="wrap_content" 
  12.             /> 
  13.         <TextView   
  14.             android:id="@+id/list_mes" 
  15.             android:layout_width="fill_parent" 
  16.             android:layout_height="wrap_content" 
  17.             /> 
  18.  
  19. </LinearLayout> 

java文件:列表里可以添加按钮图片

 
  
  1. import java.util.ArrayList;  
  2. import java.util.HashMap;  
  3. import java.util.List;  
  4. import java.util.Map;  
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.widget.ArrayAdapter;  
  8. import android.widget.ListView;  
  9. import android.widget.SimpleAdapter;  
  10.  
  11. public class AndroidtestActivity15 extends Activity{  
  12.  
  13.     private ListView list2;  
  14.     @Override 
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main15);  
  18. //************************用ArrayAdapter方法添加listview***************************************//      
  19.         //定义一个数组  
  20.         String[] arr={"孙悟空","猪八戒","牛魔王"};  
  21.         //将数组包装arrayadapter                                          android.R.layout.simple_list_item_1  
  22.         ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arr);  
  23.           
  24.         list2=(ListView) findViewById(R.id.list2);  
  25.         list2.setTextFilterEnabled(true);  
  26.         //给list2设置adapter  
  27.         list2.setAdapter(arrayAdapter);  
  28. //************************用SimpleAdapter方法添加listview***************************************//     
  29.         List<Map<String, String>> list = new ArrayList<Map<String,String>>();  
  30.         Map<String, String> date1 = new HashMap<String, String>();  
  31.             date1.put("name""java");  
  32.             date1.put("mess""是基础");  
  33.         Map<String, String> date2= new HashMap<String, String>();  
  34.             date2.put("name""android");  
  35.             date2.put("mess""j2me");  
  36.         Map<String, String> date3= new HashMap<String, String>();  
  37.             date3.put("name""jsp");  
  38.             date3.put("mess""j2ee,web方向");  
  39.         list.add(date1);  
  40.         list.add(date2);  
  41.         list.add(date3);  
  42.         SimpleAdapter simpleAdapter=new SimpleAdapter(this,   
  43.                                                       list,   
  44.                                                       R.layout.main15_2,   
  45.                                                       new String[]{"name","mess"},  
  46.                                                       new int[]{R.id.list_name,R.id.list_mes});  
  47.         ListView list3=(ListView) findViewById(R.id.list3);  
  48.         list3.setAdapter(simpleAdapter);  
  49.           
  50.           
  51.     }  
  52.