歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android ListView例子詳解

Android ListView例子詳解

日期:2017/3/1 10:40:14   编辑:Linux編程

三種實現方法,由淺入深。這中間要注意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. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/hello" />
  10. <ListView
  11. android:id="@+id/listview"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. ></ListView>
  15. </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. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.ArrayAdapter;
  5. import android.widget.ListView;
  6. public class LincListViewActivity extends Activity {
  7. private final static String[] data = {"張飛","張遼","張角","張三豐","張牙舞爪","張燈結彩","張唑啉","張大民"};
  8. //創建數據源.
  9. Zhang[] data2 = new Zhang[]{
  10. new Zhang("張飛",38,"[email protected]","燕山"),
  11. new Zhang("張遼",36,"[email protected]","雁門"),
  12. new Zhang("張角",51,"[email protected]","钜鹿"),
  13. new Zhang("張三豐",200,"[email protected]","遼東"),
  14. new Zhang("張牙舞爪",25,"[email protected]","冀州"),
  15. new Zhang("張燈結彩",25,"[email protected]","冀州") ,
  16. new Zhang("張唑啉",25,"[email protected]","冀州") ,
  17. new Zhang("張大民",25,"[email protected]","冀州") ,
  18. new Zhang("張牙舞爪",25,"[email protected]","冀州")
  19. };
  20. /** Called when the activity is first created. */
  21. @Override
  22. public void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.main);
  25. ListView listview = (ListView)findViewById(R.id.listview);
  26. /*
  27. * 第一種:普通字符串
  28. */
  29. ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
  30. android.R.layout.simple_list_item_1,data);
  31. /*
  32. * 第二種:文藝類對象
  33. */
  34. ArrayAdapter<Zhang> adapter2 = new ArrayAdapter<Zhang>(this,
  35. android.R.layout.simple_list_item_1,data2);
  36. /*
  37. * 第三種:自定義適配器
  38. */
  39. ListAdapter adapter3 = new ListAdapter(this, R.layout.listview,data2) ;
  40. listview.setAdapter(adapter3);
  41. }
  42. }
Copyright © Linux教程網 All Rights Reserved