編輯:關於 Android 程式設計
Adapter 的核心作用,是把資料處理好之後再呈現到視圖上。對一般的 ArrayAdapter 來說,只要把一個陣列與一個樣式傳給它,就能用列表把這個字串陣列顯示出來。例如:
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ls));
然而,像手機聯絡人那樣的介面,每一列除了文字之外,還可能帶有頭像與電話號碼,這種複雜的視圖是單靠 ArrayAdapter 無法完成的。此時我們可以改用 SimpleAdapter,只不過需要先自行設計版面樣式。
先建立一個 Android 專案,在 layout 目錄下定義一個 ListView 用來承載列表。
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- 定義一個 List -->
<ListView
android:id="@+id/mylist"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
SimpleAdapterTest.java:
package org.crazyit.ui;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class SimpleAdapterTest extends Activity {
private String[] names = new String[] { "虎頭", "弄玉", "李清照", "李白" };
private String[] descs = new String[] { "可愛的小孩", "一個擅長音樂的女孩", "一個擅長文學的女性", "浪漫主義詩人" };
// 這是三張圖片的 id 集合
private int[] imageIds = new int[] { R.drawable.tiger, R.drawable.nongyu, R.drawable.qingzhao, R.drawable.libai };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 創建一個 List 集合,元素是 Map
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
for (int i = 0; i < names.length; i++) {
Map<String, Object> listItem = new HashMap<String, Object>();
listItem.put("header", imageIds[i]);
listItem.put("personName", names[i]);
listItem.put("desc", descs[i]);
listItems.add(listItem);
}
// 創建一個 SimpleAdapter
SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.simple_item,
new String[] { "personName", "header", "desc" },
new int[] { R.id.name, R.id.header, R.id.desc });
ListView list = (ListView) findViewById(R.id.mylist);
list.setAdapter(simpleAdapter);
// 列表項單擊事件
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), names[position] + "被單擊了", 1).show();
}
});
list.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), names[position] + "被選中了", 1).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) { }
});
}
}
在建立 SimpleAdapter 時,我們指定了自訂樣式 R.layout.simple_item,正是靠它才能呈現出複雜的列表項。
simple_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 用於顯示頭像 -->
<ImageView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 姓名 -->
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#f0f"
android:paddingLeft="10dp" />
<!-- 描述 -->
<TextView
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:paddingLeft="10dp" />
</LinearLayout>
</LinearLayout>
把上述內容組合起來,就能得到帶有頭像、姓名與描述的複雜列表效果:

ImageView 圖像控件詳解(上):屬性辨析與縮放模式
(一)概述 ImageView 是最基礎的圖像顯示控件,除了展示圖片,還支持放大、縮小、旋轉等操作。本篇先把它的幾個核心屬性掰扯清楚。 (二)src 與 backgro
ListView 中為不同按鈕綁定各自的點擊事件
當 ListView 的每一項裡有兩個以上的按鈕時,直接為它們各寫一個 OnClickListener 很快就會亂掉。比較乾淨的做法是:在 Adapter 內部綁定點擊,
Android 手機黑名單的實現:來電攔截與短信屏蔽
有了前面幾篇關於 AIDL 與自動掛斷電話的鋪墊,這次把黑名單這個完整功能做出來。用過 Android 的都熟悉:把某些號碼拉黑之後,對方打電話進不來、發短信也不會提示。
Android 隨機驗證碼實作:用 Canvas 畫出帶干擾的驗證碼圖片
在 Android 客戶端開發中,驗證碼有兩種用途,目的完全不同:簡訊驗證碼用於身分認證,主要透過 HTTP 等通訊協議向伺服器索取並校驗;圖形隨機驗證碼則多用於「限制或