自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android ApiDemo示例解讀2:ListActivity

移動開發(fā) Android
如上一節(jié)中所講的那樣創(chuàng)建了ApiDemo工程后,我們就可以進行每個示例代碼的分析了。讀者應對Android開發(fā)有基本的了解或讀過Android開發(fā)方面的基礎教程。

如上一節(jié)中所講的那樣創(chuàng)建了ApiDemo工程后,我們就可以進行每個示例代碼的分析了。讀者應對Android開發(fā)有基本的了解或讀過Android開發(fā)方面的基礎教程。

首先是看ApiDemo的主Activity:com.example.android.apis.ApiDemos,這個主Activity為ListActivity的子類,主要用來列出ApiDemos中的200多個實例,實例采取分類層次顯示。

在ApiDemos的onCreate()中的代碼:

  1. setListAdapter(new SimpleAdapter(this, getData(path),    
  2.  android.R.layout.simple_list_item_1, new String[] { "title" },    
  3.  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 ,相當于表的列名:

Android ApiDemo示例解讀系列之二:ListActivity、SimpleAdapter和PackageManager

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) 返回一個列表。

  1. protected List getData(String prefix) {    
  2. List<Map> myData = new ArrayList<Map>();    
  3.      
  4. Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);    
  5. mainIntent.addCategory(Intent.CATEGORY_SAMPLE_CODE);    
  6.      
  7. PackageManager pm = getPackageManager();    
  8. List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);    
  9.      
  10. ... ...    
  11. for (int i = 0; i < len; i++) {    
  12. ...    
  13. if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {    
  14. addItem(myData, nextLabel, activityIntent(    
  15. info.activityInfo.applicationInfo.packageName,    
  16. info.activityInfo.name));    
  17. else {    
  18. if (entries.get(nextLabel) == null) {    
  19. addItem(myData, nextLabel,    
  20. browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));    
  21. entries.put(nextLabel, true);    
  22. }    
  23. }    
  24. }    
  25. }    
  26.      
  27. Collections.sort(myData, sDisplayNameComparator);    
  28.      
  29. return myData;    
  30. }   

它通過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信息,表示需要顯示改分類下的子類:

  1. Intent result = new Intent();    
  2. result.setClass(this, ApiDemos.class);    
  3. result.putExtra("com.example.android.apis.Path", path); 

此時如果用戶點擊改節(jié)點列表項,則會進入還分類下級目錄。

  1. protected void addItem(List<Map> data, String name, Intent intent) {    
  2. Map<String, Object> temp = new HashMap<String, Object>();    
  3. temp.put("title", name);    
  4. temp.put("intent", intent);    
  5. data.add(temp);    
  6. }    
  7.      
  8. @Override   
  9. protected void onListItemClick(ListView l, View v, int position, long id) {    
  10. Map map = (Map) l.getItemAtPosition(position);    
  11.      
  12. Intent intent = (Intent) map.get("intent");    
  13. startActivity(intent);    
  14. }   

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 ,如下:

  1. <application android:name=”ApiDemosApplication”    
  2. android:label=”@string/activity_sample_code”    
  3. android:icon=”@drawable/app_sample_code” >   
  4. …    
  5. </application>  
責任編輯:閆佳明 來源: jizhuomi
相關推薦

2013-12-19 13:40:44

Android ApiAndroid開發(fā)Android SDK

2013-12-19 14:32:31

Android ApiAndroid開發(fā)Android SDK

2013-12-19 14:34:52

Android ApiAndroid開發(fā)Android SDK

2013-12-19 14:36:43

Android ApiAndroid開發(fā)Android SDK

2013-12-19 14:13:16

Android ApiAndroid開發(fā)Android SDK

2013-12-19 14:16:46

Android ApiAndroid開發(fā)Android SDK

2013-12-19 14:28:04

Android ApiAndroid開發(fā)Android SDK

2013-12-19 14:00:39

Android ApiAndroid開發(fā)Android SDK

2013-12-19 16:26:29

Android ApiAndroid開發(fā)Android SDK

2010-01-28 10:03:19

Android Lis

2010-02-02 14:22:50

Python示例

2010-02-01 11:22:09

C++虛函數(shù)

2010-03-02 14:41:00

WCF行為控制

2010-03-05 10:47:05

Python futu

2010-02-04 16:07:39

C++回調函數(shù)

2010-01-04 17:03:27

Silverlight

2010-01-08 10:48:05

VB.NET多線程

2010-01-19 17:03:25

VB.NET可執(zhí)行語句

2010-01-05 09:57:34

.NET Framew

2010-01-28 13:56:16

Android震動
點贊
收藏

51CTO技術棧公眾號