如何在Android的listview中显示应用程序图标?
我做了一个程序来过滤用户应用程序和系统应用程序,显示到一个列表视图中,代码还可以,但我想在每个应用程序的名称前添加应用程序图标,这是我的代码,请大家帮助我如何在Android的listview中显示应用程序图标?,android,android-intent,android-widget,android-listview,Android,Android Intent,Android Widget,Android Listview,我做了一个程序来过滤用户应用程序和系统应用程序,显示到一个列表视图中,代码还可以,但我想在每个应用程序的名称前添加应用程序图标,这是我的代码,请大家帮助我 public class MainActivity extends Activity implements OnClickListener { @SuppressWarnings("rawtypes") private ArrayList results_user_app = new ArrayList(); pri
public class MainActivity extends Activity implements OnClickListener {
@SuppressWarnings("rawtypes")
private ArrayList results_user_app = new ArrayList();
private ArrayList results_sys_app = new ArrayList();
ListView lv;
Button userApp, sysApp;
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.lv);
userApp = (Button)findViewById(R.id.b_user_app);
sysApp = (Button)findViewById(R.id.b_sys_app);
userApp.setOnClickListener(this);
sysApp.setOnClickListener(this);
}
@SuppressWarnings("unchecked")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.b_user_app:
// dung de load installed user app
PackageManager pm1 = (PackageManager)this.getPackageManager();
List<ApplicationInfo> list_user_app = getPackageManager().getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
for (int n=0;n<list_user_app.size();n++) {
if((list_user_app.get(n).flags & ApplicationInfo.FLAG_SYSTEM)!=1) {
results_user_app.add(list_user_app.get(n).loadLabel(pm1).toString());
//Log.w("Installed Applications", list.get(n).loadLabel(pm).toString());
lv.setAdapter(new ArrayAdapter<ApplicationInfo>(this, android.R.layout.simple_list_item_1,results_user_app));
}
}
break;
case R.id.b_sys_app:
// dung de load installed sys app
PackageManager pm2 = (PackageManager)this.getPackageManager();
List<ApplicationInfo> list_sys_app = getPackageManager().getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
for (int n=0;n<list_sys_app.size();n++) {
if((list_sys_app.get(n).flags & ApplicationInfo.FLAG_SYSTEM)==1) {
results_sys_app.add(list_sys_app.get(n).loadLabel(pm2).toString());
//Log.w("Installed Applications", list.get(n).loadLabel(pm).toString());
lv.setAdapter(new ArrayAdapter<ApplicationInfo>(this, android.R.layout.simple_list_item_1,results_sys_app));
}
}
break;
}
}
}
您没有尝试获取应用程序图标。试试这个代码
private PackageManager pk;
pk = getPackageManager();
....
pk.getApplicationIcon(process.get(i).processName)
这对我有用