編輯:關於 Android 程式設計
ListView 是 Android 最常見的列表元件之一。本文用最簡單的 SimpleAdapter,帶你從版面配置到資料綁定走一遍,並示範如何回應某一列的點擊。
這裡需要兩個版面配置檔:一個是放置列表控制項的 Activity 版面 main.xml,另一個是每一列內容對應的 list_item.xml。要特別注意的是,若使用 ListActivity,ListView 的 id 必須採用系統內建的 android:id="@android:id/list" 形式。
<?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>
<?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>
基本思路是:先把資料放進 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 最省力的方式。
Android 自定義 View 實作仿 GitHub 的提交活躍表格
說明 本文需要一些基礎知識,如 Canvas、Paint、Path、Rect 等類的基本使用,不熟悉的同學可以先學習相關自定義 View 教程,會有很大幫助。 上圖就是
Android 如何修改 APK 打包後的預設檔名
用 Android Studio 打包時,輸出的檔案預設就叫 app-release.apk(已簽名)或 app-debug.apk(測試版)。當專案要交付給多個客戶或需
Android Material Design 實戰:打造專屬 Material 主題(二)
延續上一篇的基礎設定,這次我們把焦點放在 Material 主題真正好用的地方:調色板、觸控回饋與轉場動畫。 Material 主題帶給開發者的三件事 相較於舊有的 H
Android 更換桌布實作:WallpaperManager 三種用法與 Gallery 範例
讓使用者從預先準備的圖片裡挑一張設為桌布,是入門階段很值得動手做的小功能。Android 提供了三條路徑可以設定壁紙: WallpaperManager.setRe