3

我正在尝试为 android 手机上已安装的应用程序制作一个带有 chechboxes 的列表视图。我将图标放在 ArrayList 中,如下所示

packs = new ArrayList<String>(); // a list of all the packages
names = new ArrayList<String>(); // a list of all the apps names
icons = new ArrayList<Drawable>(); // a list of all apps drawable images
List<ApplicationInfo> packages = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
    if ((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM) !=1){ // 'if' is to remove all system applications
    packs.add((String) packageInfo.packageName);
    names.add((String) packageManager.getApplicationLabel(packageInfo));
    icons.add((Drawable) packageManager.getApplicationIcon(packageInfo));

然后把它放在listview上,我用这个。

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

icon.setImageDrawable(icons.get(position));

但是,它强制关闭,问题出在 icon.setImageDrawable(icons.get(position)); 我该如何解决?

4

1 回答 1

17

请查看我的解决方案以获取应用程序图标列表

在此获取ResolveInfo所有应用程序的列表

这是获取android中应用程序图标列表的方法,希望这个例子对你有所帮助。

导入 java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class IconListActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        loadApps();  

    }

    private void loadApps() {
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        List<ResolveInfo> mApps = getPackageManager().queryIntentActivities(mainIntent, 0);

        ListView listView = getListView();
        listView.setAdapter(new AppsAdapter(this,mApps));
     }

    public class AppsAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private List<ResolveInfo> mApps;

        public AppsAdapter(Context context, List<ResolveInfo> mApps) {
            this.inflater = LayoutInflater.from(context);
            this.mApps = mApps;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHendler hendler;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.row_view, null);
                hendler = new ViewHendler();
                hendler.textLable = (TextView)convertView.findViewById(R.id.textViewLable);
                hendler.iconImage = (ImageView)convertView.findViewById(R.id.imageViewIcon);
                convertView.setTag(hendler);
            } else {
                hendler = (ViewHendler)convertView.getTag();
            }
            ResolveInfo info = this.mApps.get(position);
            hendler.iconImage.setImageDrawable(info.loadIcon(getPackageManager()));
            hendler.textLable.setText(info.loadLabel(getPackageManager()));

            return convertView;

        }
        class ViewHendler{
            TextView textLable;
            ImageView iconImage;
        }


        public final int getCount() {
            return mApps.size();
        }

        public final Object getItem(int position) {
            return mApps.get(position);
        }

        public final long getItemId(int position) {
            return position;
        }
    }

}
于 2012-05-04T12:35:57.073 回答