【问题标题】:How to show applications icon in listview in Android?如何在 Android 的列表视图中显示应用程序图标?
【发布时间】:2013-01-17 03:29:37
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    您不是在尝试获取应用程序图标。试试这个代码

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

    这对我有用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-22
      • 2019-06-25
      • 1970-01-01
      • 2012-11-22
      • 1970-01-01
      相关资源
      最近更新 更多