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:16:46

Android ApiAndroid开发Android SDK

2013-12-19 14:13:16

Android ApiAndroid开发Android SDK

2013-12-19 14:28:04

Android ApiAndroid开发Android SDK

2013-12-19 14:00:39

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-19 17:03:25

VB.NET可执行语句

2010-01-08 10:48:05

VB.NET多线程

2010-01-05 09:57:34

.NET Framew

2010-01-28 13:56:16

Android震动
点赞
收藏

51CTO技术栈公众号