0

我目前正在制作一个应用程序,以在 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?

4

1 回答 1

0

ArrayAdapter在您的周围创建一个List<ApplicationInfo>. 覆盖getView()以填充自定义行布局中的图标和文本。

例如,此示例项目显示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));
    }
  }
于 2013-06-23T20:24:19.827 回答