I downloaded all the applications in the ListView, and next to them are two flags so that you can classify each application in category 1 or category 2. I am confused about how to enable CheckBox functionality, I read this material and am still a little suppressing. 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);
It basically works, lists all applications, displays checkboxes perfectly, I just donβt understand, where would I determine what will happen if one of the checkboxes is checked?
The main code looks like this:
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)); }
source share