Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android ListView 控件使用完全指南

編輯:關於 Android 程式設計

ListView 是 Android 開發中出鏡率極高的列表控件,幾乎每個需要展示資料集合的介面都會用到它。ListView 裡的每一個子項(Item)可以只是一個字串,也可以是一個由多個控件組成的複合版面。本文就從原理到程式碼,把 ListView 的用法完整走一遍。

ListView 的三個步驟

實現一個 ListView,大致可以拆成三步:

1. 準備好要顯示的資料;

2. 用一維或多維的動態陣列(如 ArrayList)來保存這些資料;

3. 建立一個「適配器」(Adapter)。簡單來說,適配器就是連接資料與 Item 的橋樑——動態陣列裡有多少筆資料,它就會生成多少個 Item;最後把適配器設定給 ListView 顯示出來。

理解「適配器」是掌握 ListView 的關鍵。你可以把它想像成一個翻譯官:一端對接你的資料來源,另一端對接 Item 的版面配置,負責把資料逐一填進對應的視覺元件裡。

第一步:撰寫版面 XML

首先是主版面 main.xml,內容很簡單,一個 LinearLayout 裡放一個 ListView 即可:

[plain]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  android:id="@+id/LinearLayout01"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  xmlns:android="https://schemas.android.com/apk/res/android">
  <ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/MyListView">
  </ListView>
</LinearLayout>

接著是 Item 的版面 my_listitem.xml,它決定了每一列的外觀。這裡用一個垂直 LinearLayout 包了兩個 TextView,一個當標題、一個當內容:

[html]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  android:layout_width="fill_parent"
  xmlns:android="https://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_height="wrap_content"
  android:id="@+id/MyListItem"
  android:paddingBottom="3dip"
  android:paddingLeft="10dip">
  <TextView
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:id="@+id/ItemTitle"
    android:textSize="30dip">
  </TextView>
  <TextView
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:id="@+id/ItemText">
  </TextView>
</LinearLayout>

這裡用到的幾個屬性值得說明:paddingBottom="3dip" 讓 Layout 底部留出 3 個像素的空白;paddingLeft="10dip" 讓左側留出 10 個像素;textSize="30dip" 則把標題字體設為 30 個像素大。

第二步:撰寫 Java 程式碼

最後來到 Java 部分,這裡示範用 SimpleAdapter 把資料綁定到列表:

[javascript]
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  // 綁定 XML 中的 ListView,作為 Item 的容器
  ListView list = (ListView) findViewById(R.id.MyListView);

  // 生成動態陣列並載入資料
  ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
  for(int i=0; i<30; i++){
   HashMap<String, String> map = new HashMap<String, String>();
   map.put("ItemTitle", "This is Title.....");
   map.put("ItemText", "This is text.....");
   mylist.add(map);
  }

  // 建立適配器,把陣列對應到 ListItem
  SimpleAdapter mSchedule = new SimpleAdapter(this,
    mylist,                     // 資料來源
    R.layout.my_listitem,       // ListItem 的 XML 實現
    new String[] {"ItemTitle","ItemText"}, // 動態陣列與 Item 對應的子項
    new int[] {R.id.ItemTitle, R.id.ItemText}); // Item 裡兩個 TextView 的 ID

  // 添加並顯示
  list.setAdapter(mSchedule);
}

資料來自資料庫怎麼辦

如果資料並非寫死,而是從 SQLite 資料庫讀取,做法也大同小異。先用 Cursor 遍歷查詢結果,把每一列的欄位塞進 HashMap,再交給 SimpleAdapter:

[java]
DBHelper helper=new DBHelper(this);
Cursor c=helper.query();
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
while (c.moveToNext()) {
  HashMap<String, String> map = new HashMap<String, String>();
  map.put("title", c.getString(c.getColumnIndex("Title")));
  map.put("date", c.getString(c.getColumnIndex("Date")));
  mylist.add(map);
}
String[] from={"title","date"};
int[] to={R.id.ItemTitle,R.id.ItemDate};
SimpleAdapter ada=new SimpleAdapter(this, mylist, R.layout.listitem, from, to);
ListView list = (ListView) findViewById(R.id.MyListView);
list.setAdapter(ada);

歸根結柢,無論資料來自硬編碼的陣列還是資料庫查詢,ListView 的用法都離不開「準備資料 → 建立適配器 → 設定給 ListView」這條主線。掌握了這個套路,再複雜的列表需求也能迎刃而解。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved