List of listview apps with checkboxes?

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); //more stuff for checkboxes? return convertView; } } 

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)); } //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 } 
0
source share
1 answer

In this block, you assign holder.category1 and holder.category2 instances of CheckBox or use the existing convertView, if one exists, that contains the 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 simply set the OnCheckedChangeListener for each of them as a new listener that determines what you want to do when this particular checkbox is selected.

 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 } } }); 
0
source

Source: https://habr.com/ru/post/1502159/


All Articles