編輯:Android 開發教學
前面提過 AndroidGraphics2DTutorial 是從 ListActivity 派生出來的。ListActivity 中顯示的是 ListView,而 ListView、Gallery、Spinner 都有一個共同身分:它們都是 AdapterView 的子類別。
AdapterView 的顯示是透過「資料綁定」完成的。資料源可以是陣列,也可以是資料庫記錄,而資料源與 AdapterView 之間靠 Adapter 作為橋梁。透過 Adapter,AdapterView 得以呈現資料,並處理使用者選取事件,例如點選清單中的某一項。

AndroidGraphics2DTutorial 會讀取 AndroidManifest.xml 中 Intent-Filter 符合以下條件的全部 Activity,以清單方式呈現:
<action android:name="android.intent.action.MAIN" />
<category android:name="com.pstreets.graphics2d.SAMPLE_CODE" />
它使用了 API 內建的 SimpleAdapter。以下是 AndroidGraphics2DTutorial.java 中的相關程式碼:
public class AndroidGraphics2DTutorial extends ListActivity {
private static final String SAMPLE_CATEGORY = "com.pstreets.graphics2d.SAMPLE_CODE" ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new SimpleAdapter(this, getData(),
android.R.layout.simple_list_item_1, new String[] { "title" },
new int[] { android.R.id.text1 }));
getListView().setTextFilterEnabled(true);
}
protected List getData() {
List<Map> myData = new ArrayList<Map>();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(SAMPLE_CATEGORY);
PackageManager pm = getPackageManager();
List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
if (null == list) return myData;
String[] prefixPath;
prefixPath = null;
int len = list.size();
Map<String, Boolean> entries = new HashMap<String, Boolean>();
for (int i = 0; i < len; i++) {
ResolveInfo info = list.get(i);
CharSequence labelSeq = info.loadLabel(pm);
String label = labelSeq != null ? labelSeq.toString()
: info.activityInfo.name;
String[] labelPath = label.split("/");
String nextLabel = prefixPath == null ? labelPath[0]
: labelPath[prefixPath.length];
if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {
addItem(myData, nextLabel,
activityIntent(info.activityInfo.applicationInfo.packageName,
info.activityInfo.name));
} else {
if (entries.get(nextLabel) == null) {
addItem(myData, nextLabel, browseIntent(nextLabel));
entries.put(nextLabel, true);
}
}
}
Collections.sort(myData, sDisplayNameComparator);
return myData;
}
private final static Comparator<Map> sDisplayNameComparator
= new Comparator<Map>() {
private final Collator collator = Collator.getInstance();
public int compare(Map map1, Map map2) {
return collator.compare(map1.get("title"), map2.get("title"));
}
};
protected Intent activityIntent(String pkg, String componentName) {
Intent result = new Intent();
result.setClassName(pkg, componentName);
return result;
}
protected Intent browseIntent(String path) {
Intent result = new Intent();
result.setClass(this, AndroidGraphics2DTutorial.class);
return result;
}
protected void addItem(List<Map> data, String name, Intent intent) {
Map<String, Object> temp = new HashMap<String, Object>();
temp.put("title", name);
temp.put("intent", intent);
data.add(temp);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Map map = (Map) l.getItemAtPosition(position);
Intent intent = (Intent) map.get("intent");
startActivity(intent);
}
}
為 ListActivity 中的 ListView 指定 Adapter:
setListAdapter(new SimpleAdapter(this, getData(),
android.R.layout.simple_list_item_1, new String[] { "title" },
new int[] { android.R.id.text1 }));
這個 Adapter 的資料源是 getData(),而 getData() 從 Manifest 中找出所有符合條件的示例 Activity 清單。
這裡的 DataSource 是靜態地從檔案讀取。若 DataSource 是陣列或其他來源,且程式執行中修改了內容,就應該呼叫 notifyDataSetChanged() 通知 UI 資料已變動,UI 才會刷新以反映變化。簡單說,Android 的資料綁定與 .Net WinForm、WPF 中的資料綁定概念類似。
AdapterView.OnItemClickListener() 可用來處理選取事件。對於 ListActivity,可以覆寫:
protected void onListItemClick(ListView l, View v, int position, long id)
AndroidGraphics2DTutorial 中的實作是:使用者選取某個 Activity 名稱後,啟動對應的 Activity。
上面的程式碼用 SimpleAdapter 並搭配 android.R.layout.simple_list_item_1 來顯示資料。Android 也允許自定義 Layout 來呈現資料,對這個示例而言,可以改成圖片加說明的形式,後續會介紹如何使用自定義 Adapter 與自定義 Layout 來顯示綁定的資料。
查看全套教程:https://www.bianceng.cn/OS/extra/201301/35252.htm
Android 簡明開發教程五:Activity 與它的生命週期
Android 應用中的 Activity 指的是具備螢幕顯示、能與使用者互動的基本模組,角色上近似 Java ME 中的 MIDlet,或 Windows 應用中的 F
Android 開發入門十一:TimePicker 時間選擇器實作
TimePicker 讓使用者選擇一天中的時間,可支援 24 小時制或 AM/PM 制。它常用於設定提醒、鬧鐘、排程等需要輸入時間的情境。布局與初始化在布局中加入 Tim
Android 自定義 View:用貝塞爾曲線製作流量進度條
自定義 View 的意義當系統元件無法滿足特殊視覺需求時,可以自定義 View。透過自繪方式,能做出更貼近設計稿的元件,例如流量進度條。實作思路這個流量進度條可使用畫筆、
React Native 打包卡住?用本地 Gradle 分散式檔案解決下載緩慢
初次執行 react-native run android 時,Gradle 需要下載對應版本的 gradle 套件,國內網路環境下這一步常常極慢甚至直接失敗,如下圖: