建立好ApiDemo項目後,就可以逐個樣本的來分析代碼,這裡假定讀者已對Android開發有些瞭解或是讀過Android簡明開發教程。
首先是看ApiDemo的主Activity: com.example.android.apis.ApiDemos ,這個主Activity為ListActivity的子類,主要用來列出ApiDemos中的200多個執行個體,執行個體採取分類層次顯示。
在ApiDemos 的 onCreate()中代碼:
[java]
setListAdapter(new SimpleAdapter(this, getData(path),
android.R.layout.simple_list_item_1, new String[] { "title" },
new int[] { android.R.id.text1 }));
setListAdapter(new SimpleAdapter(this, getData(path),
android.R.layout.simple_list_item_1, new String[] { "title" },
new int[] { android.R.id.text1 }));
SimpleAdatper 作為資料來源 getData(path) 與 UI ListActivity 之間的橋樑,它的建構函式如下:
SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
我們知道ListActivity可以用來顯示一個列表,在使用SimpleAdapter時可以借用二維表來更好的理解。 SimpleAdapter的資料來源data 類型為List<? extends Map<String, ?>> List 中每一項為一個Map對象,相當於二維表中一行,這一行可以有多列,每列可以有個名字,為Map<String,?> string ,相當於表的列名:
ApiDemos中每條記錄只顯示一列”title”。 android.R.layout.simple_list_item_1 為用來顯示每條記錄的Layout資源id, ListActivity允許使用自訂Layout ,這裡使用了Android系統資源,simple_list_item_1由一個TextView構成,其id為text1.
new String[] { “title” } 為需要顯示的列表的數組,ApiDemos只顯示一列“title”,如果有多列:則可以為new String[] { “title”,”field1”,”field2”,”field3” }。
new int[] { android.R.id.text1 }則指定使用 android.R.layout.simple_list_item_1 中 id 為text1的 TextView 來顯示 “title” 列。 如果有多列,Layout可以定義多個View (不一定都為TextView) ,然後為每列指定顯示的View的id。
再來看看getData(path)是如何定義的,protected List getData(String prefix) 返回一個列表
[java]
protected List getData(String prefix) {
List<Map> myData = new ArrayList<Map>();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_SAMPLE_CODE);
PackageManager pm = getPackageManager();
List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
... ...
for (int i = 0; i < len; i++) {
...
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(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));
entries.put(nextLabel, true);
}
}
}
}
Collections.sort(myData, sDisplayNameComparator);
return myData;
}
protected List getData(String prefix) {
List<Map> myData = new ArrayList<Map>();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_SAMPLE_CODE);
PackageManager pm = getPackageManager();
List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
... ...
for (int i = 0; i < len; i++) {
...
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(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));
entries.put(nextLabel, true);
}
}
}
}
Collections.sort(myData, sDisplayNameComparator);
return myData;
}
它通過PackageManager 從 AndroidManifest.xml中讀取所以Intent-Filter含有:Intent.ACTION_MAIN和Intent.CATEGORY_SAMPLE_CODE所有Activity資訊。前面說過200多個樣本根據其功能分類,比如 Hello World樣本它的Label為
App/Activity/<b>Hello <i>World</i></b>,
表是它的分類為分類App下Activity子類。getData(String prefix)根據每個Activity的Label屬性和當前層次(prefix)來決定當前列表中某項為葉子清單項目,還是分類清單項目,如果是葉子清單項目,則添加為activityIntent,當使用者點擊改清單項目時則會觸發該樣本。若是分類清單項目,則添加為browseIntent,browseIntent還是觸發ApiDemos Activity,但Intent帶有Extra資訊,表示需要顯示改分類下的子類:
[java]
Intent result = new Intent();
result.setClass(this, ApiDemos.class);
result.putExtra("com.example.android.apis.Path", path);
Intent result = new Intent();
result.setClass(this, ApiDemos.class);
result.putExtra("com.example.android.apis.Path", path);
此時如果使用者點擊改節點清單項目,則會進入還分類下級目錄。
[java]
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);
}
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);
}
addItem 給返回的List中添加一項,每個記錄含兩列 :”title”,”intent” ,其中只顯示”title”列,如果想要顯示”intent”列的資訊,就不能使用android.R.layout.simple_list_item_1 了,這時可以另外定義一個Layout 來顯示多列資料。 intent可以為觸發樣本,如”Hello World”或是下級樣本列表,此時觸發的Activity還是ApiDemos。
此外,ApiDemo還定義了ApiDemosApplication做為Application的子類,如果需要在多個Activity共用一些資料,可以定義在Application中。如果使用了自訂的Application,別忘了修改AndroidManifest.xml ,如下:
<application android:name=”ApiDemosApplication”
android:label=”@string/activity_sample_code”
android:icon=”@drawable/app_sample_code” >
…
</application>
作者:mapdigit