Android-如何在ListView中显示使用PackageManager检索的图标和文本

时间:2013-06-23 16:31:23

标签: android listview icons android-package-managers

我目前正在开发一款应用,以便在ListView中显示有关每个已安装的可启动应用的信息。但是,我无法使用文本和图标填充列表,使用;

检索
    final PackageManager pm = getPackageManager();
    List<ApplicationInfo> packages = pm
            .getInstalledApplications(PackageManager.GET_META_DATA);
    ...
   ApplicationInfo ap = pm.getApplicationInfo(packageInfo.packageName, 0 );
            name = pm.getApplicationLabel(ap);
            icon = pm.getApplicationIcon(ap);

我如何使用这些填充Listview?

1 个答案:

答案 0 :(得分:0)

ArrayAdapter周围创建List<ApplicationInfo>。覆盖getView()以填写自定义行布局中的图标和文字。

例如,this sample project显示ListView填充了所有可启动活动的条目。它的ArrayAdapter大约List<ResolveInfo>,如下所示:

class AppAdapter extends ArrayAdapter<ResolveInfo> {
    private PackageManager pm=null;

    AppAdapter(PackageManager pm, List<ResolveInfo> apps) {
      super(Launchalot.this, R.layout.row, apps);
      this.pm=pm;
    }

    @Override
    public View getView(int position, View convertView,
                          ViewGroup parent) {
      if (convertView==null) {
        convertView=newView(parent);
      }

      bindView(position, convertView);

      return(convertView);
    }

    private View newView(ViewGroup parent) {
      return(getLayoutInflater().inflate(R.layout.row, parent, false));
    }

    private void bindView(int position, View row) {
      TextView label=(TextView)row.findViewById(R.id.label);

      label.setText(getItem(position).loadLabel(pm));

      ImageView icon=(ImageView)row.findViewById(R.id.icon);

      icon.setImageDrawable(getItem(position).loadIcon(pm));
    }
  }