0

I have all downloaded apps in a ListView and next to each one are two Checkboxes so you can categorize each app in either Category1 or Category2. I'm confused with how to incorporate CheckBox functionality, I've been reading up on this stuff and it's still a little overwhelming to me. My adapter looks like this:

package com.mypackage;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.android.home.R;

import java.util.List;


public class AppInfoAdapter extends BaseAdapter {

List<PackageInfo> packageList;
Activity context;
PackageManager packageManager;

public AppInfoAdapter(Activity context, List<PackageInfo> packageList, PackageManager packageManager) {
    super();
    this.context = context;
    this.packageList = packageList;
    this.packageManager = packageManager;
}

private class ViewHolder {
    TextView apkName;
    CheckBox arcade, educational;
}

public int getCount() {
    return packageList.size();
}

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

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

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    LayoutInflater inflater = context.getLayoutInflater();

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_layout, null);
        holder = new ViewHolder();
        holder.apkName = (TextView) convertView.findViewById(R.id.appname);
        holder.category1 = (CheckBox) convertView.findViewById(R.id.category1);
        holder.category2 = (CheckBox) convertView.findViewById(R.id.category2);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    PackageInfo packageInfo = (PackageInfo) getItem(position);
    Drawable appIcon = packageManager
            .getApplicationIcon(packageInfo.applicationInfo);
    String appName = packageManager.getApplicationLabel(
            packageInfo.applicationInfo).toString();
    appIcon.setBounds(0, 0, 55, 55);
    holder.apkName.setCompoundDrawables(appIcon, null, null, null);
    holder.apkName.setCompoundDrawablePadding(15);
    holder.apkName.setText(appName);
    //more stuff for checkboxes?
    return convertView;
}

}

That basically works, lists all apps, displays checkboxes fine, I just don't understand where I'd define what happens if one of the checkboxes is checked?

Main code looks like this for now:

public class ScanApps extends Activity {
PackageManager packageManager;
ListView apkList;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scan_apps);

    {packageManager = getPackageManager();
    List<PackageInfo> packageList = packageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS);
    List<PackageInfo> installedapps = new ArrayList<PackageInfo>();

    for(PackageInfo apps: packageList){
        if(!isSystemPackage(apps)){
            installedapps.add(apps);
        }
    }
    Collections.sort(installedapps, new Comparator<PackageInfo>() {
        public int compare(PackageInfo o1, PackageInfo o2) {
            return o1.applicationInfo.loadLabel(getPackageManager()).toString().compareToIgnoreCase(o2.applicationInfo.loadLabel(getPackageManager()).toString());
        }
    });
    apkList = (ListView) findViewById(R.id.listApps);
    apkList.setAdapter(new AppInfoAdapter(this, installedapps, packageManager));
} //this code loads all installed Android Apps into a list



}


private boolean isSystemPackage(PackageInfo pkgInfo) {
    return ((pkgInfo.applicationInfo.flags & android.content.pm.ApplicationInfo.FLAG_SYSTEM) != 0) ? true
            : false;
} //excludes system apps

}
scibor
  • 983
  • 4
  • 12
  • 21
  • http://stackoverflow.com/questions/16685366/customised-listview-using-arrayadapter-class-in-android/16686623#16686623. check this for reference. – Raghunandan Jun 13 '13 at 15:10

1 Answers1

0

In this block you assign holder.category1 and holder.category2 to CheckBox instances, or use the existing convertView if it exists, which contains a holder object:

if (convertView == null) {
    convertView = inflater.inflate(R.layout.list_layout, null);
    holder = new ViewHolder();
    holder.apkName = (TextView) convertView.findViewById(R.id.appname);
    holder.category1 = (CheckBox) convertView.findViewById(R.id.category1);
    holder.category2 = (CheckBox) convertView.findViewById(R.id.category2);
    convertView.setTag(holder);
} else {
    holder = (ViewHolder) convertView.getTag();
}

Right below that, you can just set the OnCheckedChangeListener for each to be a new listener that defines whatever you want to do when that particular checkbox is checked.

holder.category1.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if ( isChecked )
        {
           // Do some stuff
        }

    }
});

holder.category2.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if ( isChecked )
        {
           // Do other stuff
        }

    }
});
mattgmg1990
  • 5,576
  • 4
  • 21
  • 26
  • Perfect, I thought it might be something simple like that, it was just difficult to discern when all I had to work with was other people's completed code. I do have a follow up question though: in the if statement, I'm plugging in holder.category1.isChecked() and the IDE prompts me to make holder final. Is there anything I should be careful about in doing so or is this ok? – scibor Jun 13 '13 at 15:35
  • No problem. Yes, that is a requirement in Java because you are creating an anonymous class instance that implements the OnCheckedChangeListener interface. In order for your `holder` instance to be accessible to this anonymous class implementation, you need to make it final. That just means that you can't assign it to something else after you assign it the first time. It's no problem to just declare it `final`. – mattgmg1990 Jun 13 '13 at 16:06