3

I am trying to make a listview with chechboxes for the installed applications on the android phone. I put the icons in an ArrayList as follows

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

Then to put it on the listview, i use this.

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

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

However, it force closes and The problem is in icon.setImageDrawable(icons.get(position)); How can I solve it?

Ananth
  • 2,597
  • 1
  • 29
  • 39
Nora Harb
  • 31
  • 1
  • 2
  • Use `adb logcat`, DDMS, or the DDMS perspective in Eclipse to examine LogCat and look at the stack trace associated with your "force close". In particular, look at the name of the exception that was raised. If you are getting a `NullPointerException` on the line you mentioned, try cleaning your project -- Project > Clean from the Eclipse main menu, or `ant clean` for command-line builds. – CommonsWare May 03 '12 at 17:12
  • a bit late but take a look at this one its good http://www.androprogrammer.com/2013/10/list-view-with-check-box-using-custom.html – buckau Jul 27 '14 at 01:06

1 Answers1

17

Please check out my solution for getting the list of icon of application

In this getting the list of ResolveInfo of all application

This is the way to get the list of icon of application in android, hope this example will be helpful for you.

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

}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Ashish Dwivedi
  • 8,048
  • 5
  • 58
  • 78