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

Android ListView 入門:兩種使用方式與 android.R.id.List 報錯的真相

編輯:關於 Android 程式設計

ListView 是 Android 早期最常使用、也最容易被誤解的元件之一。它的使用流程固定由三個部分組成:準備資料、建立 Adapter、把資料綁上 ListView。而在入門階段,幾乎每個人都會撞上一個經典錯誤:

Your content must have a ListView whose id attribute is "android.R.id.List"

網路上流傳的解法是「把 android:id="@+id/list" 改成 android:id="@+id/android:list"」。這確實能消除報錯,但它其實不是原因,只是結果——真正的原因是:你採用的使用方式與程式碼寫法不匹配。

兩種 ListView 使用方式

方式一:使用 Android 內建的 ListActivity

這種寫法讓 Activity 本身繼承 ListActivity,系統會自動尋找 id 為 @android:id/list 的 ListView 並與之關聯。此時:

  • 版面配置中的 ListView id 必須寫成 @android:id/list(也可寫 @+id/android:list)。
  • Java 端不需要呼叫 findViewById(),而是直接呼叫 setListAdapter()。

主版面:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false"
        android:scrollbars="vertical" />
</LinearLayout>

Java 端:

public class MainActivity extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 注意:此處不用 findViewById 取得 ListView

        ArrayList<HashMap<String, String>> list = new ArrayList<>();
        HashMap<String, String> map1 = new HashMap<>();
        map1.put("key", "張三");
        map1.put("value", "學生");
        HashMap<String, String> map2 = new HashMap<>();
        map2.put("key", "李四");
        map2.put("value", "老師");
        list.add(map1);
        list.add(map2);

        SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.hashmap,
            new String[]{"key", "value"},
            new int[]{R.id.key, R.id.value});

        setListAdapter(adapter);
    }
}

方式二:自訂 ListView

若 Activity 繼承的是普通的 Activity,系統不會幫你尋找任何特定 id 的 ListView,這時:

  • ListView 的 id 寫成 @+id/list 即可,叫什麼都可以。
  • 必須自己用 findViewById() 取得 ListView,再呼叫 setAdapter()。
listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);

資料準備與 SimpleAdapter 的建構方式與前一個例子完全相同。

報錯的真正原因

把兩種方式並排看就很清楚了:那個錯誤訊息只會出現在你繼承了 ListActivity,卻在版面中找不到 @android:id/list 的情況。如果你的 Activity 繼承的是 Activity,就算把 id 寫成 @+id/list 也不會報這個錯。

因此排查順序應該是:

  1. 先確認 Activity 繼承的是 ListActivity 還是 Activity。
  2. 若是 ListActivity,檢查版面中 ListView 的 id 是否為 @android:id/list。
  3. 若是 Activity,檢查是否已用 findViewById() 取得 ListView 並呼叫 setAdapter()。

列表項的版面配置

兩種方式共用同一份 item layout,這裡用水平排列的兩個 TextView 示範:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/linelayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/key"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#aa0000" />

    <TextView
        android:id="@+id/value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

SimpleAdapter 的最後兩組陣列就是「資料中的 key 名稱」與「要填入的 View id」的一一對應表,這也是為什麼 item layout 中的 id 必須和 new int[]{...} 完全一致。

釐清這層關係之後,ListView 的入門門檻其實不高。真正複雜的議題(View 回收、ViewHolder 優化、多類型項目)則要等熟悉基本綁定流程之後再來處理。

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