문제

I'm currently making an app to display information about every installed, launchable app in a ListView. However, I am having trouble populating the list with both text and icon, which are retrieved using;

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

How would I go about populating a Listview with these?

도움이 되었습니까?

해결책

Create an ArrayAdapter around your List<ApplicationInfo>. Override getView() to fill in the icon and text in your custom row layout.

For example, this sample project shows a ListView filled with entries for all of the launchable activities. It has a ArrayAdapter around a List<ResolveInfo> that looks like this:

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));
    }
  }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top