歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發應用實例:ListView的應用

Android開發應用實例:ListView的應用

日期:2017/3/1 10:36:30   编辑:Linux編程

該程序完成如下功能:
1 在ListView中顯示多個學生的名字。
2 點擊ListView中的條目,查詢並顯示該學生的年齡、性別、照片等信息。

本文要用到的相關圖片下載:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2012年資料/1月/29日/Android開發應用實例:ListView的應用/

效果圖:

650) this.width=650;" height=120>

student.java

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

ListViewActivity.java

  1. package com.lingdududu.listview;
  2. import java.util.ArrayList;
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.content.DialogInterface;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.AdapterView;
  9. import android.widget.AdapterView.OnItemClickListener;
  10. import android.widget.ArrayAdapter;
  11. import android.widget.ListView;
  12. /*
  13. * @author lingdududu
  14. * 該程序的主要功能是點擊ListView的學生姓名,就能彈出對話框
  15. * 在對話框裡面顯示學生的詳細信息:圖片,姓名,性別,年齡
  16. */
  17. public class ListViewActivity extends Activity {
  18. private ListView lv;
  19. @Override
  20. public void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.main);
  23. //學生圖片的ID數組
  24. final int[] stu_pic = {
  25. R.drawable.pic1,
  26. R.drawable.pic2,
  27. R.drawable.pic3,
  28. R.drawable.pic4,
  29. R.drawable.pic5,
  30. R.drawable.pic6};
  31. final AlertDialog.Builder builder = new AlertDialog.Builder(this);
  32. lv=(ListView)findViewById(R.id.list);
  33. /*生成動態數組,加入數據*/
  34. final ArrayList<Student>students = new ArrayList<Student>();
  35. students.add(new Student("小張","女",21));
  36. students.add(new Student("小明","男",22));
  37. students.add(new Student("小王","男",23));
  38. students.add(new Student("小麗","女",21));
  39. students.add(new Student("小紅","女",22));
  40. students.add(new Student("小周","男",23));
  41. ArrayAdapter<Student> adapter = new ArrayAdapter<Student>
  42. (this,//布局文件
  43. android.R.layout.simple_list_item_1,//android.R.layout.simple_list_item_1,系統定義的布局文件
  44. students);//數據來源
  45. //為ListView設置適配器
  46. lv.setAdapter(adapter);
  47. lv.setOnItemClickListener(new OnItemClickListener() {
  48. //arg0 發生點擊動作的AdapterView
  49. //arg1 在AdapterView中被點擊的視圖(它是由adapter提供的一個視圖)
  50. //arg2 視圖在adapter中的位置
  51. //arg3 被點擊元素的行id
  52. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  53. long arg3){
  54. //將學生的詳細信息在對話框裡面顯示
  55. builder.setMessage(students.get(arg2).getStuIfo())
  56. //顯示學生的圖片
  57. .setIcon(stu_pic[arg2])
  58. .setTitle("你好,這是我的詳細信息!")
  59. .setPositiveButton("確定", new DialogInterface.OnClickListener() {
  60. public void onClick(DialogInterface dialog, int which) {
  61. }
  62. });
  63. //創建對話框
  64. AlertDialog ad = builder.create();
  65. //顯示對話框
  66. ad.show();
  67. }
  68. });
  69. }
  70. }

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>
Copyright © Linux教程網 All Rights Reserved