Android ListView例子详解


三种实现方法,由浅入深。这中间要注意Adapter的用法,其实你要是看过Android的文档,你会发现有很多Adapter,

如果你还不太清楚适配器模式,可以先补补这方面的知识。在实际工作中,设计模式是个很好的帮手。

两个layout文件:

main.xml

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

listview.xml

[html]
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout    
  3.   xmlns:android="http://schemas.android.com/apk/res/android"    
  4.   android:id="@+id/linerlayout1"    
  5.   android:orientation="vertical"    
  6.   android:layout_height="fill_parent"    
  7.   android:layout_width="fill_parent"    
  8.   >    
  9.      <TextView    
  10.         android:id="@+id/person_name"    
  11.         android:textSize="23sp"    
  12.         android:layout_width="wrap_content"    
  13.         android:layout_height="wrap_content"    
  14.      />    
  15.      <TextView    
  16.         android:id="@+id/person_age"    
  17.         android:layout_width="wrap_content"    
  18.         android:layout_height="wrap_content"    
  19.     />    
  20.     <TextView    
  21.         android:id="@+id/person_email"    
  22.         android:layout_width="wrap_content"    
  23.         android:layout_height="wrap_content"    
  24.     />    
  25.     <TextView    
  26.         android:id="@+id/person_address"    
  27.         android:layout_width="wrap_content"    
  28.         android:layout_height="wrap_content"    
  29.     />    
  30. </LinearLayout>    
Activity:LincListViewActivity.java

[java]
  1. package com.linc.listview;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.ArrayAdapter;  
  6. import android.widget.ListView;  
  7.   
  8. public class LincListViewActivity extends Activity {  
  9.     private final static String[] data = {"张飞","张辽","张角","张三丰","张牙舞爪","张灯结彩","张唑啉","张大民"};  
  10.       
  11.     //创建数据源.     
  12.     Zhang[] data2 = new Zhang[]{    
  13.         new Zhang("张飞",38,"zhangfei@gmail.com","燕山"),    
  14.         new Zhang("张辽",36,"zhangliao@sina.com","雁门"),    
  15.         new Zhang("张角",51,"zhangjiao@gmail.com","钜鹿"),    
  16.         new Zhang("张三丰",200,"sanfeng@gmail.com","辽东"),    
  17.         new Zhang("张牙舞爪",25,"5zhao@gmail.com","冀州"),  
  18.         new Zhang("张灯结彩",25,"5zhao@gmail.com","冀州") ,  
  19.         new Zhang("张唑啉",25,"5zhao@gmail.com","冀州") ,  
  20.         new Zhang("张大民",25,"5zhao@gmail.com","冀州") ,  
  21.         new Zhang("张牙舞爪",25,"5zhao@gmail.com","冀州")    
  22.     };    
  23.     /** Called when the activity is first created. */  
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.main);  
  28.           
  29.         ListView listview = (ListView)findViewById(R.id.listview);  
  30.         /* 
  31.          * 第一种:普通字符串 
  32.          */  
  33.         ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,   
  34.                 android.R.layout.simple_list_item_1,data);  
  35.           
  36.         /* 
  37.          * 第二种:文艺类对象 
  38.          */  
  39.         ArrayAdapter<Zhang> adapter2 = new ArrayAdapter<Zhang>(this,   
  40.                 android.R.layout.simple_list_item_1,data2);  
  41.           
  42.         /* 
  43.          * 第三种:自定义适配器 
  44.          */  
  45.         ListAdapter adapter3 = new ListAdapter(this, R.layout.listview,data2) ;  
  46.           
  47.         listview.setAdapter(adapter3);  
  48.     }  
  49. }  
  • 1
  • 2
  • 下一页

相关内容