ListView是Android一系列组件之一,它以垂直列表的方式列出所需的列表项。实现方式有两种:
 * 第一种:使用ListView组件
 * 第二种:当前的Activity继承ListActivity
     ListView是需要绑定数据源的----->ArrayAdapter、SimpleAdapter、SimpleCursorAdapter
需要注意的是SimpleCursorAdapter要以数据库作为数据源的时候才使用。
来自SDK的描述:
A view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view.
接下来就三种不同的数据源我写了一个例子,和大家一起来好好学习一下ListView。
首先,新建一个Android项目--->取名ListViewDemo
MainActivity.java的代码如下:
- public class MainActivity extends Activity implements OnClickListener{  
 -  private Button btnArray,btnSimple,btnSimpleCursor;  
 -  /** Called when the activity is first created. */  
 -  @Override  
 -  public void onCreate(Bundle savedInstanceState) {  
 -  super.onCreate(savedInstanceState);  
 -  setContentView(R.layout.main);  
 -    
 -  getWidgetInstance();  
 -    
 -  btnArray.setOnClickListener(this);  
 -  btnSimple.setOnClickListener(this);  
 -  btnSimpleCursor.setOnClickListener(this);  
 -  }  
 -    
 -  public void getWidgetInstance() {  
 -  btnArray = (Button)findViewById(R.id.btnArray);  
 -  btnSimple = (Button)findViewById(R.id.btnSimple);  
 -  btnSimpleCursor = (Button)findViewById(R.id.btnSimpleCursor);  
 -  }  
 -  
 -  @Override  
 -  public void onClick(View v) {  
 -  int id = v.getId();  
 -  switch(id) {  
 -  case R.id.btnArray:  
 -  Intent intent_array = new Intent(MainActivity.this,ArrayAdapterActivity.class);  
 -  MainActivity.this.startActivity(intent_array);  
 -  break;  
 -  case R.id.btnSimple:  
 -  Intent intent_simple = new Intent(MainActivity.this,SimpleAdapterActivity.class);  
 -  MainActivity.this.startActivity(intent_simple);  
 -  break;  
 -  case R.id.btnSimpleCursor:  
 -  Intent intent_cursor = new Intent(MainActivity.this,SimpleCursorAdapterActivity.class);  
 -  MainActivity.this.startActivity(intent_cursor);  
 -  break;  
 -  }  
 -  }  
 - } 
 
main.xml的布局:
- <?xml version="1.0" encoding="utf-8"?> 
 - <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 -     android:orientation="vertical" 
 -     android:layout_width="fill_parent" 
 -     android:layout_height="fill_parent" 
 -     > 
 - <TextView    
 -     android:layout_width="fill_parent"   
 -     android:layout_height="wrap_content"   
 -     android:text="ListView使用示例" 
 -     android:gravity="center" 
 -     android:textColor="#EEE" 
 -     android:textStyle="bold" 
 -     android:textSize="25sp" 
 -     /> 
 - <Button 
 -     android:id="@+id/btnArray" 
 -     android:layout_width="wrap_content"   
 -     android:layout_height="wrap_content"   
 -     android:text="show ArrayAdapter" 
 -     /> 
 - <Button 
 -     android:id="@+id/btnSimple" 
 -     android:layout_width="wrap_content"   
 -     android:layout_height="wrap_content"   
 -     android:text="show SimpleAdapter" 
 -     /> 
 - <Button 
 -     android:id="@+id/btnSimpleCursor" 
 -     android:layout_width="wrap_content"   
 -     android:layout_height="wrap_content"   
 -     android:text="show SimpleCursorAdapter" 
 -     /> 
 - </LinearLayout> 
 
显示效果:主界面有三个Button,每一个Button对应相应的数据源,显示出ListActivity。
接着,编写三个不同数据源的Activity
第一种:ArrayAdapter
ArrayAdapter是三种数据源中最简单的Adapter,只能显示一行文字
ArrayAdapter.java的代码如下:
- public class ArrayAdapterActivity extends ListActivity {  
 -  @Override  
 -  public void onCreate(Bundle savedInstanceState) {  
 -  super.onCreate(savedInstanceState);  
 -  //列表项的数据  
 -  String[] strs = {"1","2","3","4","5"};  
 -  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,strs);  
 -  setListAdapter(adapter);  
 -  }  
 - } 
 
第二种:SimpleAdapterActivity
SimpleAdapterActivity需要自定义每一项显示的布局方式:
所以simple.xml的代码如下:
- <?xml version="1.0" encoding="utf-8"?> 
 - <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 -     android:orientation="vertical" 
 -     android:layout_width="fill_parent" 
 -     android:layout_height="fill_parent" 
 -     > 
 - <ImageView 
 -     android:id="@+id/img" 
 -     android:layout_width="wrap_content" 
 -     android:layout_height="wrap_content" 
 -     android:layout_margin="5dp" 
 -     /> 
 - <TextView 
 -     android:id="@+id/title" 
 -     android:layout_width="wrap_content"   
 -     android:layout_height="wrap_content"   
 -     android:textColor="#ffffff" 
 -     android:textSize="20sp" 
 -     /> 
 - </LinearLayout> 
 
SimpleAdapterActivity.java代码如下:
- public class SimpleAdapterActivity extends ListActivity {  
 -  @Override  
 -  public void onCreate(Bundle savedInstanceState) {  
 -  super.onCreate(savedInstanceState);  
 -    
 -  SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.simple, new String[] { "title", "img" }, new int[] { R.id.title, R.id.img });  
 -  setListAdapter(adapter);  
 -  }  
 -    
 -  private List<Map<String, Object>> getData() {  
 -  //map.put(参数名字,参数值)  
 -  List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();  
 -  Map<String, Object> map = new HashMap<String, Object>();  
 -  map.put("title", "摩托罗拉");  
 -  map.put("img", R.drawable.icon);  
 -  list.add(map);  
 -    
 -  map = new HashMap<String, Object>();  
 -  map.put("title", "诺基亚");  
 -  map.put("img", R.drawable.icon);  
 -  list.add(map);  
 -    
 -  map = new HashMap<String, Object>();  
 -  map.put("title", "三星");  
 -  map.put("img", R.drawable.icon);  
 -  list.add(map);  
 -  return list;  
 -  }   
 - } 
 
第三种:SimpleCursorAdapterActivity
SimpleCursorAdapterActivity是依赖于数据库的一种数据源,这里为了方便我就不重新建一个数据库,然后插入数据了,我就用读取联系人姓名的方式为例和大家做一个简单的介绍。
SimpleCursorAdapterActivity.xml代码如下:
- public class SimpleCursorAdapterActivity extends ListActivity {  
 -  @Override  
 -  public void onCreate(Bundle savedInstanceState) {  
 -  super.onCreate(savedInstanceState);  
 -  //获得一个指向系统通讯录数据库的Cursor对象获得数据来源  
 -  Cursor cur = getContentResolver().query(People.CONTENT_URI, null, null, null, null);  
 -  startManagingCursor(cur);  
 -  //实例化列表适配器  
 -    
 -  ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cur, new String[] {People.NAME}, new int[] {android.R.id.text1});  
 -  setListAdapter(adapter);  
 -  }  
 - } 
 
这里特别需要注意的一点是:不要忘了在AndroidManifest.xml文件中加入权限
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>