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

ListActivity 完全指南:版面配置規則與三種 Adapter 綁定方式

編輯:關於 Android 程式設計

清單大概是行動 App 裡最常見的元件。Android 提供 ListView 直接完成這件事,而 ListActivity 則在它之上包了一層便利:繼承它之後,只要指定 Adapter 就能把資料攤上螢幕,不必處處理找 View 與設定 Adapter 的樣板程式。

版面配置的兩條硬性規則

ListActivity 對版面有明確要求:

  • 版面中必須包含一個且僅一個 ListView。
  • 該 ListView 的 id 必須是 @id/android:list(也可寫 @android:id/list)。ListActivity 靠這個固定 id 去尋找清單控制項。
  • 若想在使用者清單為空時顯示提示文字,可以再放一個 TextView,其 id 必須為 @id/android:empty,內容透過 android:text 指定。

標準範本如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="8dp"
    android:paddingRight="8dp">

    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#00FF00"
        android:drawSelectorOnTop="false" />

    <TextView
        android:id="@id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FF0000"
        android:text="No data" />
</LinearLayout>

單項外觀:系統範本或自訂版面

版面搞定後,還有一個問題沒決定:每一列要長什麼樣?Android 內建了幾個現成範本可從 android.R.layout 選用:

  • simple_list_item_1:單一 TextView,最常用。
  • simple_list_item_2:兩個 TextView,適合標題加副標。
  • simple_list_item_checked:帶勾選狀態的單項。
  • simple_list_item_single_choice:單選清單。
  • simple_list_item_multiple_choice:多選清單。

如果這些都不合用,自訂一個 layout 檔案即可,裡面可以放 ImageView、多行文字或任何 View。做過 ASP.NET 開發的人可以把 ListView 想成 DataList:資料來源、樣板、綁定三者分離,概念是相通的。

Adapter:資料與畫面之間的橋樑

要把資料塞進清單,一定要透過 Adapter。最常搭配 ListView 的有三種。

ArrayAdapter:最簡單的陣列來源

String[] data = { "Item1", "Item2", "Item3", "Item4", "Item5" };
setListAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, data));

適合資料本身就是字串陣列或 List<String> 的情境,程式碼一行解決。

SimpleCursorAdapter:把資料庫游標攤成清單

// 從資料庫取得 Cursor
Cursor cursor = adapter.getAllNotes();
startManagingCursor(cursor);

// 要顯示的資料庫欄位
String[] from = new String[] {
        DiaryDbAdapter.KEY_COLUMN_TITLE,
        DiaryDbAdapter.KEY_COLUMN_CREATEED };

// 對應到自訂版面中的 View id
int[] to = new int[] { R.id.txtRowTitle, R.id.txtRowCreateed };

SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
        R.layout.diaryrow, cursor, from, to);
setListAdapter(notes);

from 與 to 兩個陣列必須等長且順序對應,第一個欄位填進第一個 View,依此類推。這是初學者最常出錯的地方。

SimpleAdapter:以 Map 清單做彈性呈現

需要「一列顯示多個不同型別欄位」時,SimpleAdapter 是最合適的選擇。它的資料來源是 List<? extends Map<String, ?>>,清單中的每一項對應畫面上的一列,Map 中的每個鍵則對應列內的一個 View。

SimpleAdapter(Context context,
              List<? extends Map<String, ?>> data,
              int resource,
              String[] from,
              int[] to)

四個參數的意義:context 一般傳 Activity.this;data 是資料來源;resource 是自訂版面;from 指定要讀取哪些 Map 鍵;to 指定要塞進哪些 View。

List<Hashtable<String, Object>> listContent
        = new ArrayList<Hashtable<String, Object>>();

for (int i = 0; i < deviceList.size(); i++) {
    Hashtable<String, Object> table = new Hashtable<String, Object>();
    table.put("name", deviceList.get(i).Name);
    table.put("address", deviceList.get(i).Address);
    table.put("type", deviceList.get(i).Type + "");
    listContent.add(table);
}

SimpleAdapter adapter = new SimpleAdapter(HeartActivity.this,
        listContent,
        R.layout.child,                       // 自訂版面
        new String[] { "name", "address" },   // Map 的鍵
        new int[] { R.id.txtDeviceName, R.id.txtDeviceAddress }); // 目標 View

setListAdapter(adapter);

怎麼選

三者可以這樣判斷:資料是純字串序列就用 ArrayAdapter;資料來自 SQLite 且欄位與畫面基本一一對應,用 SimpleCursorAdapter;需要客製化呈現或資料結構較複雜,就用 SimpleAdapter。至於大量資料與需要複用 View 的高效能場景,則應直接繼承 BaseAdapter 自行實作 getView(),並在裡面套用 view holder 模式。

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