当前位置:操作系统 > 安卓/Android >>

Android ApiDemos示例解析(2): SimpleAdapter,ListActivity,PackageManager

创建好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
 

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,