您的位置:首页 > 移动开发 > Android开发

Android得到系统已安装应用程序包列表方法 自定义ListView显示 PackageManager的使用

2012-06-12 14:27 981 查看
得到系统安装的程序包,可以通过PackageManager对象getInstalledPackages方法,该方法直接返回一个包含程序包信息PackageInfo的List。今天学习PackageManager的同时,顺便记一下ListView使用自定义适配器以及自定义视图的方法,前面写得不够详细。先看效果图:




每一项分成三部分,左边是应用图标,右边分上下两部分,上面是应用名,下面是包名。顺便说一下,ListView的自定义布局,定义的是一项的布局,然后根据项的数量叠加。

下面是这个布局的xml代码 piitem.xml:

1
2
3
4
5
6
7
8
9
10
11
12

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/icon" android:layout_width="48dip"
android:layout_height="48dip" />
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:id="@+id/appName" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="@+id/packageName" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>

main.xml的布局就不贴了吧,就一个ListView,id为lv

写个自定义的适配器:

1
2
3
4
5
6
7
8
9
10
11
1213
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

package com.pocketdigi;
import java.util.List;
import java.util.Map;
 
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
 
public class Adapter extends SimpleAdapter {
private int[] mTo;
private String[] mFrom;
private ViewBinder mViewBinder;
private List<? extends Map<String, ?>> mData;
private int mResource;
private LayoutInflater mInflater;
public Adapter(Context context,List<? extends Map<String, ?>> data, int resource, String[] from,int[] to) {
super(context, data, resource, from, to);
mData = data;
mResource = resource;
mFrom = from;
mTo = to;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
 
public View getView(int position, View convertView, ViewGroup parent) {
return createViewFromResource(position, convertView, parent, mResource);
}
private View createViewFromResource(int position, View convertView,
ViewGroup parent, int resource) {
View v;
if (convertView == null) {
v = mInflater.inflate(resource, parent, false);
 
final int[] to = mTo;
final int count = to.length;
final View[] holder = new View[count];
 
for (int i = 0; i < count; i++) {
holder[i] = v.findViewById(to[i]);
}
v.setTag(holder);
} else {
v = convertView;
}
bindView(position, v);
return v;
}
 
private void bindView(int position, View view) {
final Map dataSet = mData.get(position);
if (dataSet == null) {
return;
}
 
final ViewBinder binder = mViewBinder;
final View[] holder = (View[]) view.getTag();
final String[] from = mFrom;
final int[] to = mTo;
final int count = to.length;
 
for (int i = 0; i < count; i++) {
final View v = holder[i];
if (v != null) {
final Object data = dataSet.get(from[i]);
String text = data == null ? "" : data.toString();
if (text == null) {
text = "";
}
 
boolean bound = false;
if (binder != null) {
bound = binder.setViewValue(v, data, text);
}
 
if (!bound) {
//自定义适配器,关键在这里,根据传过来的控件类型以及值的数据类型,执行相应的方法
//可以根据自己需要自行添加if语句。另CheckBox等继承自TextView的控件也会被识别成TextView, 这就需要判断值的类型了
if (v instanceof TextView) {
//如果是TextView控件
setViewText((TextView) v, text);
//调用SimpleAdapter自带的方法,设置文本
} else if (v instanceof ImageView) {//如果是ImageView控件
setViewImage((ImageView) v, (Drawable) data);
//调用下面自己写的方法,设置图片
} else {
throw new IllegalStateException(v.getClass().getName() + " is not a " +
" view that can be bounds by this SimpleAdapter");
}
}
}
}
}
 
public void setViewImage(ImageView v, Drawable value) {
v.setImageDrawable(value);
 
}
 
};

关键部分已注释,如果用到其他控件,只要修改注释的地方增加判断就可以了。

下面是主程序代码 Main.java:

1
2
3
4
5
6
7
8
9
10
11
1213
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

package com.pocketdigi;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
 
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.ListView;
 
public class Main extends Activity {
/** Called when the activity is first created. */
ListView lv;
Adapter adapter;
ArrayList<HashMap<String, Object>> items=new ArrayList<HashMap<String, Object>>();
 
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.lv);
PackageManager  pm= getPackageManager();
//得到PackageManager对象
List<PackageInfo> packs = pm.getInstalledPackages(0);
//得到系统 安装的所有程序包的PackageInfo对象
 
for (PackageInfo pi : packs) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("icon", pi.applicationInfo.loadIcon(pm));
//图标
map.put("appName", pi.applicationInfo.loadLabel(pm));
//应用名
map.put("packageName", pi.packageName);
//包名
items.add(map);
//循环读取存到HashMap,再增加到ArrayList.一个HashMap就是一项
}
 
adapter = new Adapter(this, items, R.layout.piitem, new String[] {
"icon", "appName", "packageName" }, new int[] { R.id.icon,
R.id.appName, R.id.packageName });
//参数:Context,ArrayList(item的集合),item的layout,包含ArrayList中Hashmap的key的数组,key所对应的值相对应的控件id
lv.setAdapter(adapter);
 
}
}

摘自:http://www.pocketdigi.com/20110728/425.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐