Android组件之ListView(列表视图)


ListView列表视图,为列表添加列表项有两种方法,下面用一个列子分别介绍:

在样式文件中:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="名单:"  
  11.         />  
  12.       
  13.     <ListView  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:entries="@array/sports"  
  17.         android:divider="#00FF00"  
  18.         />  
  19.       
  20.     <ListView  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:id="@+id/nameList"  
  24.         android:divider="#00FF00"  
  25.         />  
  26.   
  27. </LinearLayout>  
在上面的布局中定义了两个ListView,第一个ListView通过android:entries指定了列表的项数组:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string-array name="sports" >  
  4.         <item>足球</item>  
  5.         <item>篮球</item>  
  6.         <item>乒乓球</item>  
  7.         <item>网球</item>       
  8.     </string-array>  
  9. </resources>  
第二个ListView通过ArrayAdapter适配器使用数组来确定列表项,并监听点击事件,每点击用日志输出打印:
  1. package cn.class3g.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6. import android.view.View;  
  7. import android.widget.AdapterView;  
  8. import android.widget.AdapterView.OnItemClickListener;  
  9. import android.widget.ArrayAdapter;  
  10. import android.widget.ListView;  
  11.   
  12. public class ListViewDemo extends Activity  
  13.     implements OnItemClickListener{  
  14.   
  15.     ListView nameList = null;  
  16.     String[] names = {"张三","李四","王五","宋六","猪八"};  
  17.       
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         this.setContentView(R.layout.list_layout);  
  21.         findViews();  
  22.     }  
  23.   
  24.     private void findViews() {  
  25.         nameList = (ListView) this.findViewById(R.id.nameList);  
  26.         //定义一个适配器,同时将定义列表项的数组添加进去  
  27.         ArrayAdapter adapter = new ArrayAdapter(this,  
  28.                 android.R.layout.simple_list_item_1,names);  
  29.         nameList.setAdapter(adapter);  
  30.         //为列表添加监听事件  
  31.         nameList.setOnItemClickListener(this);  
  32.     }  
  33.   
  34.     //覆盖监听器接口OnItemClickListener的抽象方法  通过日志打印所点击的列表项信息  
  35.     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {  
  36.         Log.i("TAG", names[arg2]  
  37.                 + " position=" + String.valueOf(arg2)  
  38.                 +"  row_id=" + String.valueOf(arg3) );  
  39.     }  
  40.   
  41. }  
模拟器与日志显示和输出效果:


相关内容