Android ApiDemo示例解讀2:ListActivity
如上一節(jié)中所講的那樣創(chuàng)建了ApiDemo工程后,我們就可以進行每個示例代碼的分析了。讀者應對Android開發(fā)有基本的了解或讀過Android開發(fā)方面的基礎教程。
首先是看ApiDemo的主Activity:com.example.android.apis.ApiDemos,這個主Activity為ListActivity的子類,主要用來列出ApiDemos中的200多個實例,實例采取分類層次顯示。
在ApiDemos的onCreate()中的代碼:
- setListAdapter(new SimpleAdapter(this, getData(path),
- android.R.layout.simple_list_item_1, new String[] { "title" },
- new int[] { android.R.id.text1 }));
SimpleAdatper 作為數(shù)據(jù)源 getData(path) 與 UI ListActivity 之間的橋梁,它的構造函數(shù)如下:
SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
我們知道ListActivity可以用來顯示一個列表,在使用SimpleAdapter時可以借用二維表來更好的理解。 SimpleAdapter的數(shù)據(jù)源data 類型為List<? extends Map<String, ?>> List 中每一項為一個Map對象,相當于二維表中一行,這一行可以有多列,每列可以有個名字,為Map<String,?> string ,相當于表的列名:
ApiDemos中每條記錄只顯示一列”title”。 android.R.layout.simple_list_item_1 為用來顯示每條記錄的Layout資源id, ListActivity允許使用自定義Layout ,這里使用了Android系統(tǒng)資源,simple_list_item_1由一個TextView構成,其id為text1。
new String[] { “title” } 為需要顯示的列表的數(shù)組,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) 返回一個列表。
- 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多個示例根據(jù)其功能分類,比如 Hello World示例它的Label為
App/Activity/<b>Hello <i>World</i></b>,
表示它的分類為分類App下Activity子類。getData(String prefix)根據(jù)每個Activity的Label屬性和當前層次(prefix)來決定當前列表中某項為葉子列表項,還是分類列表項,如果是葉子列表 項,則添加為activityIntent,當用戶點擊改列表項時則會觸發(fā)該示例。若是分類列表項,則添加為 browseIntent,browseIntent還是觸發(fā)ApiDemos Activity,但Intent帶有Extra信息,表示需要顯示改分類下的子類:
- Intent result = new Intent();
- result.setClass(this, ApiDemos.class);
- result.putExtra("com.example.android.apis.Path", path);
此時如果用戶點擊改節(jié)點列表項,則會進入還分類下級目錄。
- 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 來顯示多列數(shù)據(jù)。 intent可以為觸發(fā)示例,如”Hello World”或是下級示例列表,此時觸發(fā)的Activity還是ApiDemos。
此外,ApiDemo還定義了ApiDemosApplication做為Application的子類,如果需要在多個Activity共享一些數(shù)據(jù), 可以定義在Application中。如果使用了自定義的Application,別忘了修改AndroidManifest.xml ,如下:
- <application android:name=”ApiDemosApplication”
- android:label=”@string/activity_sample_code”
- android:icon=”@drawable/app_sample_code” >
- …
- </application>