Problem with checkbox in installed application in listview

I referred to these links link , link , link , link

but my problem is not resolved. I know there are many questions regarding this problem, but my problem

I have grade 4 A, B, C, D

From class A, I goto class B, where all installed applications are listed. in class B, send Packagelist to class c (BaseAdapter class) and then the user who selected the user will go to class A.

So my problem is where and how can I use the POJO class to transfer installed applications to the baseadapter class, and then how can I get all the checked value (application string name) and pass it to class A. as well as how I can support checked flag status.

any help plz.

custom adapter class

import java.util.List; import android.app.Activity; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.graphics.drawable.Drawable; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.TextView; import android.widget.Toast; public class AppListAdapter extends BaseAdapter { List<PackageInfo> packageList; Activity context; PackageManager packageManager; boolean[] itemChecked; int checkBoxCounter = 0; int checkBoxInitialized = 0; public AppListAdapter(Activity context, List<PackageInfo> packageList, PackageManager packageManager) { super(); this.context = context; this.packageList = packageList; this.packageManager = packageManager; itemChecked = new boolean[packageList.size()]; } private class ViewHolder { TextView apkName; CheckBox ck1; } public int getCount() { return packageList.size(); } public Object getItem(int position) { return packageList.get(position); } public long getItemId(int position) { return 0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { // final ViewHolder holder; checkBoxCounter = 0; checkBoxInitialized = 0; LayoutInflater inflater = context.getLayoutInflater(); if (convertView == null) { convertView = inflater.inflate(R.layout.apklist_item, null); final ViewHolder holder = new ViewHolder(); holder.apkName = (TextView) convertView .findViewById(R.id.ApkList_tvappname); holder.ck1 = (CheckBox) convertView .findViewById(R.id.ApkList_checkBox); holder.ck1 .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { AppsSelected element = (AppsSelected) holder.ck1 .getTag(); element.setSelected(buttonView.isChecked()); if (checkBoxCounter <= checkBoxInitialized) { // increment counter, when we scroll the List it // execute onCheckedChanged everytime so by // using this stuff we can maintain the state checkBoxCounter++; } else { element = (AppsSelected) holder.ck1.getTag(); element.setSelected(buttonView.isChecked()); if (element.isSelected()) Toast.makeText( context, "You selected " + element.getName(), Toast.LENGTH_LONG).show(); else Toast.makeText( context, "Not selected " + element.getName(), Toast.LENGTH_LONG).show(); } } }); convertView.setTag(holder); holder.ck1.setTag(packageList.get(position)); } else { ((ViewHolder) convertView.getTag()).ck1.setTag(packageList.get(position)); } ViewHolder 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, 40, 40); holder.apkName.setCompoundDrawables(appIcon, null, null, null); holder.apkName.setCompoundDrawablePadding(15); holder.apkName.setText(appName); return convertView; } } 
+4
source share
1 answer

You can refer to my answer here, what you can do is the state of the storage in a boolean array, then you will not encounter this problem .....

Problem with default Android profile

+4
source

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


All Articles