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

ListView 基本用法入門:SimpleAdapter 快速上手

編輯:關於 Android 程式設計

ListView 是 Android 最常見的列表元件之一。本文用最簡單的 SimpleAdapter,帶你從版面配置到資料綁定走一遍,並示範如何回應某一列的點擊。

這裡需要兩個版面配置檔:一個是放置列表控制項的 Activity 版面 main.xml,另一個是每一列內容對應的 list_item.xml。要特別注意的是,若使用 ListActivity,ListView 的 id 必須採用系統內建的 android:id="@android:id/list" 形式。

main.xml

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
        android:layout_height="match_parent"
        android:padding="20dip"/>
</LinearLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/user_name"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
    <TextView
        android:id="@+id/user_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
</LinearLayout>

MainActivity

基本思路是:先把資料放進 ArrayList,再用 SimpleAdapter 完成綁定:

package com.example.android_newlistview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.app.ListActivity;
import android.widget.SimpleAdapter;

public class MainActivity extends ListActivity {

    // ListView 每一列的鍵名
    String[] from = {"name", "id"};
    // 對應 list_item 中控件的 id
    int[] to = {R.id.user_name, R.id.user_id};

    String[] userName = {"zhangsan", "lisi", "wangwu", "zhaoliu"}; // 第一列顯示的人名
    String[] userId = {"1001", "1002", "1003", "1004"};            // 對應的 ID

    ArrayList<HashMap<String, String>> list = null;
    HashMap<String, String> map = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        list = new ArrayList<HashMap<String, String>>();

        // ListView 的一行對應一個 HashMap,以列名為鍵、該列的值為 Value
        for (int i = 0; i < 4; i++) {
            map = new HashMap<String, String>(); // 有幾列就建幾個 map,避免空指針
            map.put("id", userId[i]);
            map.put("name", userName[i]);
            list.add(map);
        }

        // 建立 SimpleAdapter 並套用
        SimpleAdapter adapter =
            new SimpleAdapter(this, list, R.layout.list_item, from, to);
        setListAdapter(adapter);
    }
}

回應某一列的點擊

若想針對點擊某一列做出回應,只要覆寫 onListItemClick 方法即可,參數中的 position 從 0 開始,代表被點擊的那一列:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    // 在這裡依 position 處理點擊邏輯
}

透過 SimpleAdapter,你只要準備好「資料清單+from/to 對應關係」,就能快速把資料呈現在列表上,是入門 ListView 最省力的方式。

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