如何在Android的listview中显示应用程序图标?

时间:2013-01-17 03:29:38

标签: android android-intent android-widget android-listview

我做了一个程序来过滤用户应用程序和系统应用程序,将它显示到列表视图中,代码是好的,但我想在每个应用程序的名称之前添加应用程序图标,这是我的代码,请大家帮助我!

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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您没有尝试获取应用程序图标。试试这段代码

  private PackageManager pk;
  pk = getPackageManager();
  ....
  pk.getApplicationIcon(process.get(i).processName)

这对我有用