成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Android ApiDemo示例解讀2:ListActivity

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

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

首先是看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 作為數據源 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 ,相當于表的列名:

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

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

  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多個示例根據其功能分類,比如 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信息,表示需要顯示改分類下的子類:

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

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

  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 來顯示多列數據。 intent可以為觸發示例,如”Hello World”或是下級示例列表,此時觸發的Activity還是ApiDemos。

此外,ApiDemo還定義了ApiDemosApplication做為Application的子類,如果需要在多個Activity共享一些數據, 可以定義在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開發Android SDK

2013-12-19 14:32:31

Android ApiAndroid開發Android SDK

2013-12-19 14:34:52

Android ApiAndroid開發Android SDK

2013-12-19 14:36:43

Android ApiAndroid開發Android SDK

2013-12-19 14:13:16

Android ApiAndroid開發Android SDK

2013-12-19 14:16:46

Android ApiAndroid開發Android SDK

2013-12-19 14:00:39

Android ApiAndroid開發Android SDK

2013-12-19 14:28:04

Android ApiAndroid開發Android SDK

2013-12-19 16:26:29

Android ApiAndroid開發Android SDK

2010-01-28 10:03:19

Android Lis

2010-02-02 14:22:50

Python示例

2010-02-01 11:22:09

C++虛函數

2010-03-02 14:41:00

WCF行為控制

2010-03-05 10:47:05

Python futu

2010-02-04 16:07:39

C++回調函數

2010-01-04 17:03:27

Silverlight

2010-01-08 10:48:05

VB.NET多線程

2010-01-19 17:03:25

VB.NET可執行語句

2010-01-05 09:57:34

.NET Framew

2010-01-28 13:56:16

Android震動
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久一区二区三区四区 | 天天人人精品 | 日本精品免费在线观看 | 男女污网站 | 亚洲天堂久久新 | 国产乱码精品一区二区三区五月婷 | av在线播放不卡 | 欧美又大粗又爽又黄大片视频 | 国产成人精品一区二区三区在线 | a级黄色毛片免费播放视频 国产精品视频在线观看 | 久久er99热精品一区二区 | 天天爱av | 一级全黄少妇性色生活免费看 | 草久视频| 日本国产高清 | 一本大道久久a久久精二百 国产成人免费在线 | 国产亚洲一区二区三区在线 | 久久99视频这里只有精品 | 欧美一区久久 | 婷婷丁香激情 | 日韩欧美在线播放 | 99精品视频在线 | 99reav| 亚洲国产一区二区三区, | 国产精品永久久久久久久www | 亚洲欧美另类在线观看 | 日韩一区二区在线视频 | 精品国产一区一区二区三亚瑟 | 亚洲欧洲视频 | 国产精品日韩欧美一区二区 | 伊人网综合 | 国产一区二区三区高清 | 亚洲精品一区在线观看 | 91一区二区三区在线观看 | 亚洲一一在线 | 天堂网avav | 电影午夜精品一区二区三区 | 免费视频久久 | 日日骚网| 一本综合久久 | 国产在线观看一区二区三区 |