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

Android ListView 基本用法:SimpleAdapter 綁定自訂版面

編輯:關於 Android 程式設計

ListView 大概是 Android 最常使用的列表元件。最經典的組合是 ListActivity + SimpleAdapter:資料放進 ArrayList<HashMap>,再指定每一列要用哪個版面檔、哪些欄位對應到哪些控制項,就能快速把清單顯示出來。

步驟一:列表本身的版面(listview.xml)

如果 Activity 繼承 ListActivity,版面中的 ListView 一定要使用系統保留的 id @id/android:list。

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

    <LinearLayout
        android:id="@+id/listLinearlayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ListView
            android:id="@id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </ListView>

    </LinearLayout>
</LinearLayout>

步驟二:單列的版面(user.xml)

這一列要顯示兩個 TextView:使用者名稱與 IP。

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

    <TextView
        android:id="@+id/user_name"
        android:layout_width="160dip"
        android:layout_height="wrap_content"
        android:paddingLeft="2dip"
        android:paddingTop="2dip"
        android:textSize="10pt"/>

    <TextView
        android:id="@+id/user_ip"
        android:layout_width="160dip"
        android:layout_height="wrap_content"
        android:paddingRight="2dip"
        android:paddingTop="2dip"
        android:textSize="10pt"/>

</LinearLayout>

步驟三:ListViewMain.java

package com.example.wenandroid;

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

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

public class ListViewMain extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);

        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>();

        HashMap<String, String> map1 = new HashMap<String, String>();
        map1.put("user_name", "張三");
        map1.put("user_ip", "192.168.0.110");

        HashMap<String, String> map2 = new HashMap<String, String>();
        map2.put("user_name", "李四");
        map2.put("user_ip", "192.168.0.111");

        list.add(map1);
        list.add(map2);

        SimpleAdapter listAdapter = new SimpleAdapter(this, list, R.layout.user,
                new String[] { "user_name", "user_ip" },
                new int[] { R.id.user_name, R.id.user_ip });

        setListAdapter(listAdapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        System.out.println("id=" + id);
        System.out.println("position=" + position);
    }
}

參數怎麼讀?

SimpleAdapter 的建構子共有五個參數,依序是:

1. Context:目前執行環境。
2. data:資料來源,這裡是 List<Map>,每個 Map 代表一列。
3. resource:單列的版面檔。
4. from:Map 中的鍵名稱陣列。
5. to:要填入值的控制項 id 陣列,順序必須與 from 一一對應。

理解這個對應關係之後,只要調整 Map 的鍵與版面中的 id,就能自由變化清單的呈現方式,不必改動任何程式邏輯。

延伸提醒

SimpleAdapter 適合結構單純的清單。一旦每一列需要顯示圖片、需要非同步載入、或是版面有部分是自訂 View,就應該改用繼承 BaseAdapter 的自訂 Adapter,並在 getView() 中實作「版面重用」(透過 convertView 與 ViewHolder 模式),否則長清單的捲動會明顯卡頓。

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