该程序完成如下功能:
ListView中显示多个学生的名字。
点击ListView中的条目,查询并显示该学生的年龄、性别、照片等信息。

效果图:

 student.java

 
  
  1. package com.lingdududu.listview;  
  2.  
  3. public class Student {  
  4.  
  5.     public String sname;  
  6.     public String ssex;  
  7.     public int sage;  
  8.       
  9.     Student(String name,String sex,int age){  
  10.         sname=name;  
  11.         ssex=sex;  
  12.         sage=age;  
  13.     }  
  14.       
  15.     void Stuedent(String name){  
  16.         sname=name;  
  17.     }  
  18.       
  19.     public String getName(){  
  20.         return sname;  
  21.     }  
  22.       
  23.     public String getSex(){  
  24.         return ssex;  
  25.     }  
  26.       
  27.     public int getAge(){  
  28.         return sage;  
  29.     }  
  30.     public String toString (){  
  31.         return sname;  
  32.     }  
  33.      
  34.     //这个方法返回学生的姓名,性别,年龄  
  35.     public String getStuIfo(){  
  36.         return ("姓名:"+sname+"\n性别:"+ssex+"\n年龄:"+sage);  
  37.     }  
  38. }  

ListViewActivity.java

 
  
  1. package com.lingdududu.listview;  
  2.  
  3. import java.util.ArrayList;  
  4. import android.app.Activity;  
  5. import android.app.AlertDialog;  
  6. import android.content.DialogInterface;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.widget.AdapterView;  
  10. import android.widget.AdapterView.OnItemClickListener;  
  11. import android.widget.ArrayAdapter;  
  12. import android.widget.ListView;  
  13. /*  
  14.  * @author lingdududu  
  15.  * 该程序的主要功能是点击ListView的学生姓名,就能弹出对话框  
  16.  * 在对话框里面显示学生的详细信息:图片,姓名,性别,年龄  
  17.  */ 
  18. public class ListViewActivity extends Activity {  
  19.     private ListView lv;  
  20.     @Override 
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.           
  25.         //学生图片的ID数组  
  26.         final int[] stu_pic = {  
  27.                    R.drawable.pic1,  
  28.                    R.drawable.pic2,  
  29.                    R.drawable.pic3,  
  30.                    R.drawable.pic4,  
  31.                    R.drawable.pic5,  
  32.                    R.drawable.pic6};  
  33.           
  34.         final AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  35.           
  36.         lv=(ListView)findViewById(R.id.list);  
  37.           
  38.         /*生成动态数组,加入数据*/ 
  39.         final ArrayList<Student>students = new ArrayList<Student>();  
  40.           
  41.         students.add(new Student("小张","女",21));  
  42.         students.add(new Student("小明","男",22));  
  43.         students.add(new Student("小王","男",23));  
  44.         students.add(new Student("小丽","女",21));  
  45.         students.add(new Student("小红","女",22));  
  46.         students.add(new Student("小周","男",23));  
  47.           
  48.                 
  49.         ArrayAdapter<Student> adapter = new ArrayAdapter<Student>  
  50.                (this,//布局文件   
  51.                 android.R.layout.simple_list_item_1,//android.R.layout.simple_list_item_1,系统定义的布局文件   
  52.                 students);//数据来源    
  53.         //为ListView设置适配器  
  54.         lv.setAdapter(adapter);  
  55.         lv.setOnItemClickListener(new OnItemClickListener() {  
  56.               
  57.             //arg0  发生点击动作的AdapterView  
  58.             //arg1 在AdapterView中被点击的视图(它是由adapter提供的一个视图)  
  59.             //arg2 视图在adapter中的位置  
  60.             //arg3 被点击元素的行id  
  61.             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
  62.                     long arg3){  
  63.             //将学生的详细信息在对话框里面显示  
  64.             builder.setMessage(students.get(arg2).getStuIfo())  
  65.             //显示学生的图片  
  66.             .setIcon(stu_pic[arg2])  
  67.             .setTitle("你好,这是我的详细信息!")             
  68.             .setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  69.                 public void onClick(DialogInterface dialog, int which) {  
  70.                       
  71.                 }         
  72.             });  
  73.             //创建对话框  
  74.             AlertDialog ad = builder.create();  
  75.             //显示对话框  
  76.             ad.show();  
  77.             }  
  78.         });       
  79.     }  

 main.xml

 
  
  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/list" 
  9.    android:layout_width="fill_parent"   
  10.    android:layout_height="wrap_content"   
  11.    /> 
  12. </LinearLayout>