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

Android ListView 全面實戰:從基礎清單到圖文列表

編輯:關於 Android 程式設計

ListView 是 Android 最常用來呈現資料列表的元件。本文從最簡單的字串清單出發,逐步帶到讀取聯絡人、以及搭配 SimpleAdapter 顯示圖文並包的進階用法,幫助你快速掌握 ListView 的各項操作。

一、最基礎的清單

先在版面配置檔中放上一個 ListView,這裡順便示範了分隔線、背景與快取顏色提示等屬性:

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@drawable/icon"
        android:cacheColorHint="#00000000"
        android:background="@drawable/icon66" >
    </ListView>
</LinearLayout>

接著在 Activity 中用 ArrayAdapter 把字串陣列塞進去:

public class ListView1Activity extends Activity {
    String[] shujuStrings = { "wwww", "wwww", "erfff" };

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

        ListView listView1 = (ListView) findViewById(R.id.listView1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_list_item_1,
                shujuStrings);
        listView1.setAdapter(adapter);
    }
}

二、讀取手機聯絡人

透過 ContentResolver 取得聯絡人 Cursor,再用 SimpleCursorAdapter 綁定到列表上,是 ListView 與系統資料結合的常見寫法:

public class ListViewActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListView listView = new ListView(this);

        // 取得手機聯絡人
        @SuppressWarnings("deprecation")
        Cursor cursor = getContentResolver()
                .query(People.CONTENT_URI, null, null, null, null);
        startManagingCursor(cursor);

        ListAdapter listAdapter = new SimpleCursorAdapter(
                this,
                android.R.layout.simple_expandable_list_item_1,
                cursor, new String[] { People.NAME },
                new int[] { android.R.id.text1 });
        listView.setAdapter(listAdapter);

        setContentView(listView);
    }
}

這裡也可以改用自建的資料來源(例如 getData() 回傳大量測試字串)搭配 ArrayAdapter 來驗證滾動與效能表現。

三、顯示圖文並包的清單

當每一列不只有文字,還需要圖片與按鈕時,改用 SimpleAdapter 並自訂一列的配置檔。資料以 Map 列表的形式提供:

public class ListViewActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SimpleAdapter adapter = new SimpleAdapter(
                this, getData(),
                R.layout.main,
                new String[] { "title", "info", "img", "button1" },
                new int[] { R.id.title, R.id.info, R.id.img, R.id.button1 });
        setListAdapter(adapter);
    }

    private List<? extends Map<String, ?>> getData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("title", "G1");
        map.put("info", "google 1");
        map.put("img", R.drawable.drive);
        map.put("button1", "刪除第1行");
        list.add(map);
        // ... 依此類推加入更多列 ...
        return list;
    }
}

對應的單列版面配置,用水平的 LinearLayout 把圖片、文字區塊與按鈕排在一起:

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

    <ImageView
        android:id="@+id/img"
        android:layout_width="72px"
        android:layout_height="72px"
        android:layout_margin="5px" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFFFF"
            android:textSize="22px" />
        <TextView
            android:id="@+id/info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFFFF"
            android:textSize="13px" />
    </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </Button>
</LinearLayout>

從 ArrayAdapter 到 SimpleCursorAdapter,再到 SimpleAdapter,ListView 透過不同 Adapter 就能靈活適應各種資料型態與版面需求。本文原始作者為 gongzibai。

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