My goal is to display the list of installed apps in a custom listview (within an Activity) without checkbox getting unchecked (when listview is scrolled).
The Problem: When Listview is scrolled, the checked status of the checkbox becomes unchecked.
My Progress So Far:
While this blog entry clearly shows how to get rid of the problem of checkboxes in listview getting unchecked, the difference is that it uses Model as the custom class whereas in my case it is a system class [i.e. the packageList1 = packageManager.getInstalledPackages(0);]
How can I resolve this? Below is my code attempt. I am able to successfully display apps in a listviw with checkboxes, just that checked status is getting unchecked when listview is scrolled.
I AM OPEN TO ANY METHOD AS AN ANSWER THAT WILL ENABLE ME TO SHOW LIST OF INSTALLED APPS IN A LISTVIEW WITHOUT THE CHECKED STATUS OF CHECKBOXES BECOMING UNCHECKED
My Custom Listadapter class:
public class Listadapter extends BaseAdapter {
private static String TAG = "focus";
List<PackageInfo> packageList;
Activity context;
PackageManager packageManager;
boolean[] itemChecked;
List<String> appNamestoBlock_local;
public Listadapter(Activity context, List<PackageInfo> packageList,
PackageManager packageManager, List<String> appNamestoBlock) {
super();
this.context = context;
this.packageList = packageList;
this.packageManager = packageManager;
this.appNamestoBlock_local = appNamestoBlock;
itemChecked = new boolean[packageList.size()];
}
private String getSerializedBlockedAppNames() {
String serializedBlockedAppNames;
Log.v(TAG,
"-------- List<String> list = "
+ TextUtils.join(",", appNamestoBlock_local));
serializedBlockedAppNames = TextUtils.join(",", appNamestoBlock_local);
return serializedBlockedAppNames;
}
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;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.apkName = (TextView) convertView
.findViewById(R.id.textView1);
holder.ck1 = (CheckBox) convertView.findViewById(R.id.checkBox1);
holder.ck1.setFocusable(false);
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, 40, 40);
holder.apkName.setCompoundDrawables(appIcon, null, null, null);
holder.apkName.setCompoundDrawablePadding(15);
holder.apkName.setText(appName);
holder.ck1.setChecked(false);
if (itemChecked[position])
holder.ck1.setChecked(true);
else
holder.ck1.setChecked(false);
// Log.v(TAG,
// "-------- checking in getView() in ListAdapter if appName is in getSerializedBlockedAppNames()");
if (getSerializedBlockedAppNames().contains(appName)) {
Log.v(TAG + " ListAdapter",
"-------- YES, appName is in getSerializedBlockedAppNames(), appName = "
+ appName);
holder.ck1.setChecked(true);
}
holder.ck1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
itemChecked[position] = true;
} else {
itemChecked[position] = false;
}
}
});
return convertView;
}
}
In onCreate function of my main activity:
PackageManager packageManager = getPackageManager();
List<PackageInfo> packageList1 = packageManager.getInstalledPackages(0);
Listadapter Adapter = new Listadapter(MainActivityCircularSeekbar.this, packageList1, packageManager, blockedApps);
See Question&Answers more detail:
os