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

ListView 的資料骨幹:ArrayAdapter 是怎麼把資料送上畫面的

編輯:關於 Android 程式設計

做小練習時又碰到 Adapter,才發現自己一直沒好好整理過它。這次就用最基礎的 ArrayAdapter 為例,把整個資料流走一遍。

Adapter 的定位

一句話:Adapter 是資料與視圖之間的橋樑。資料在 Adapter 這層被整理成型,再由 ListView 逐行取出、貼到版面配置的視圖上。

Android 內建的 Adapter 家族相當龐大,常見的有 ArrayAdapter、BaseAdapter、CursorAdapter、SimpleAdapter、SimpleCursorAdapter、ResourceCursorAdapter、HeaderViewListAdapter、SpinnerAdapter、WrapperListAdapter 等。其中 BaseAdapter 是最靈活的基底,ArrayAdapter 則是最容易上手的起點。

版面配置

上半部是輸入區(姓名、地址與一顆儲存鈕),下半部留給 ListView。

<?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" >

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name:" />
        <EditText
            android:id="@+id/name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Address:" />
        <EditText
            android:id="@+id/addr"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <Button
        android:id="@+id/save"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Save" />
</LinearLayout>

Java 端

public class LunchList extends Activity {

    List<Restaurant> model = new ArrayList<Restaurant>();
    ArrayAdapter<Restaurant> adapter = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button save = (Button) findViewById(R.id.save);
        save.setOnClickListener(onSave);

        ListView list = (ListView) findViewById(R.id.restaurants);
        adapter = new ArrayAdapter<Restaurant>(this,
                android.R.layout.simple_list_item_1, model);
        list.setAdapter(adapter);   // 把配好的適配器交給 ListView
    }

    private View.OnClickListener onSave = new View.OnClickListener() {
        public void onClick(View v) {
            Restaurant r = new Restaurant();
            r.setName(((EditText) findViewById(R.id.name)).getText().toString());
            r.setAddress(((EditText) findViewById(R.id.addr)).getText().toString());

            RadioGroup types = (RadioGroup) findViewById(R.id.types);
            switch (types.getCheckedRadioButtonId()) {
                case R.id.sit_down: r.setType("sit_down"); break;
                case R.id.take_out: r.setType("take_out"); break;
                case R.id.delivery: r.setType("delivery"); break;
            }
            adapter.add(r);   // 每次新增的項目都進到適配器裡
        }
    };
}

逐點說明

1. Adapter 的角色:資料與視圖之間的橋樑, ListView 自己不碰資料,全部透過 Adapter 取得。

2. 泛型參數:ArrayAdapter<Restaurant> 表示這個適配器管理的是 Restaurant 物件陣列。泛型也可以是字串、數字或任何自訂類別。

3. 三個建構參數:

adapter = new ArrayAdapter<Restaurant>(this,
        android.R.layout.simple_list_item_1, model);
  • 第一個是 Context,這裡就是當前的 Activity。
  • 第二個是「每一列長什麼樣」的版面資源。此處用的是 SDK 內建的 simple_list_item_1,裡面只有一個 TextView。
  • 第三個是要顯示的資料集合。宣告時 model 還是空的,資料稍後透過 adapter.add(r) 一筆筆補進去。

4. 整體流程:ListView 拿到這三個資訊後,會遍歷資料集合,每讀出一筆就 inflate 一次指定版面並填入內容,逐列堆疊成我們看到的清單。

下一步

ArrayAdapter 只會呼叫 TextView 的 setText(),一旦列上要放圖片或多個欄位,就得換成繼承 BaseAdapter 自訂 getView()。那時你會遇到另一個重要課題:複用 convertView 與ViewHolder 模式,這才是清單捲動順不順的關鍵。另外在現今專案中,ListView 多已讓位給 RecyclerView,但 Adapter 的核心概念是完全相通的。

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