編輯:關於 Android 程式設計
清單大概是行動 App 裡最常見的元件。Android 提供 ListView 直接完成這件事,而 ListActivity 則在它之上包了一層便利:繼承它之後,只要指定 Adapter 就能把資料攤上螢幕,不必處處理找 View 與設定 Adapter 的樣板程式。
ListActivity 對版面有明確要求:
ListView。ListView 的 id 必須是 @id/android:list(也可寫 @android:id/list)。ListActivity 靠這個固定 id 去尋找清單控制項。TextView,其 id 必須為 @id/android:empty,內容透過 android:text 指定。標準範本如下:
<?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"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00FF00"
android:drawSelectorOnTop="false" />
<TextView
android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000"
android:text="No data" />
</LinearLayout>
版面搞定後,還有一個問題沒決定:每一列要長什麼樣?Android 內建了幾個現成範本可從 android.R.layout 選用:
simple_list_item_1:單一 TextView,最常用。simple_list_item_2:兩個 TextView,適合標題加副標。simple_list_item_checked:帶勾選狀態的單項。simple_list_item_single_choice:單選清單。simple_list_item_multiple_choice:多選清單。如果這些都不合用,自訂一個 layout 檔案即可,裡面可以放 ImageView、多行文字或任何 View。做過 ASP.NET 開發的人可以把 ListView 想成 DataList:資料來源、樣板、綁定三者分離,概念是相通的。
要把資料塞進清單,一定要透過 Adapter。最常搭配 ListView 的有三種。
String[] data = { "Item1", "Item2", "Item3", "Item4", "Item5" };
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, data));
適合資料本身就是字串陣列或 List<String> 的情境,程式碼一行解決。
// 從資料庫取得 Cursor
Cursor cursor = adapter.getAllNotes();
startManagingCursor(cursor);
// 要顯示的資料庫欄位
String[] from = new String[] {
DiaryDbAdapter.KEY_COLUMN_TITLE,
DiaryDbAdapter.KEY_COLUMN_CREATEED };
// 對應到自訂版面中的 View id
int[] to = new int[] { R.id.txtRowTitle, R.id.txtRowCreateed };
SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.diaryrow, cursor, from, to);
setListAdapter(notes);
from 與 to 兩個陣列必須等長且順序對應,第一個欄位填進第一個 View,依此類推。這是初學者最常出錯的地方。
需要「一列顯示多個不同型別欄位」時,SimpleAdapter 是最合適的選擇。它的資料來源是 List<? extends Map<String, ?>>,清單中的每一項對應畫面上的一列,Map 中的每個鍵則對應列內的一個 View。
SimpleAdapter(Context context,
List<? extends Map<String, ?>> data,
int resource,
String[] from,
int[] to)
四個參數的意義:context 一般傳 Activity.this;data 是資料來源;resource 是自訂版面;from 指定要讀取哪些 Map 鍵;to 指定要塞進哪些 View。
List<Hashtable<String, Object>> listContent
= new ArrayList<Hashtable<String, Object>>();
for (int i = 0; i < deviceList.size(); i++) {
Hashtable<String, Object> table = new Hashtable<String, Object>();
table.put("name", deviceList.get(i).Name);
table.put("address", deviceList.get(i).Address);
table.put("type", deviceList.get(i).Type + "");
listContent.add(table);
}
SimpleAdapter adapter = new SimpleAdapter(HeartActivity.this,
listContent,
R.layout.child, // 自訂版面
new String[] { "name", "address" }, // Map 的鍵
new int[] { R.id.txtDeviceName, R.id.txtDeviceAddress }); // 目標 View
setListAdapter(adapter);
三者可以這樣判斷:資料是純字串序列就用 ArrayAdapter;資料來自 SQLite 且欄位與畫面基本一一對應,用 SimpleCursorAdapter;需要客製化呈現或資料結構較複雜,就用 SimpleAdapter。至於大量資料與需要複用 View 的高效能場景,則應直接繼承 BaseAdapter 自行實作 getView(),並在裡面套用 view holder 模式。
Android 進階教程:Git 本地操作完全詳解
標籤(空格分隔):Android 進階 一、引言 在此前的 Android 基礎入門系列里,我們已經帶大家初步玩過 Git。而在進階系列中,我們會系統地把 Git 梳理一
Android 網路請求:URL 組裝與回應解析全流程
最近在做 Android 網路相關的開發,順手把請求流程整理成一篇筆記。整體脈絡其實很單純:用戶端向伺服器發出一個請求(也就是所謂的 POST),送出一段完整的 URL;
Android 開發:APK 反編譯與防止反編譯(代碼混淆)
防止反編譯是每个開發者的必修課。辛辛苦苦做出來的應用,被別人三兩下反編譯並改頭換面,是件尷尬又洩氣的事。那麼該怎麼防?這裡用 Google Android 自帶的代碼混淆
Android ListView 圓角效果實作解析
在介面設計中,直角與圓角各有適用場景。相較於稜角分明的列表,圓角設計通常更柔和,也更容易營造輕鬆活潑的視覺感受。本文示範如何為 Android 的 ListView 加上