I've been stuck for 2 days trying to solve an error with the displayed checkboxes in my app. I've created an activity that displays list of installed apps with checkboxes on the side of each displayed app. When I click on a checkbox of a specific app e.g Facebook some of other apps' checkboxes get marked without them been clicked. I don't know why this is happening, I'll appreciate it if you help me :D. I've used a list view with array adapter to display the list of installed apps.
ArrayAdapter
public class ListofAppsAdapter extends ArrayAdapter<ApplicationInfo> {
private List<ApplicationInfo> applicationInfoList;
private Context context;
private PackageManager packageManager;
private int x = 0;
SparseBooleanArray mCheckStates;
public ListofAppsAdapter(@NonNull Context context, int resource, @NonNull List<ApplicationInfo> objects) {
super(context, resource, objects);
this.context = context;
this.applicationInfoList = objects;
packageManager = context.getPackageManager();
mCheckStates = new SparseBooleanArray(applicationInfoList.size());
}
@Override
public int getCount() {
return ((null != applicationInfoList) ? applicationInfoList.size() : 0);
}
@Nullable
@Override
public ApplicationInfo getItem(int position) {
return ((null != applicationInfoList) ? applicationInfoList.get(position) : null);
}
@Override
public long getItemId(int position) {
return position;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.listsofapps_layout, null);
}
ApplicationInfo data = applicationInfoList.get(position);
if (data != null) {
ImageView app_logo = view.findViewById(R.id.appicon);
CheckBox checkBox = view.findViewById(R.id.add_app);
app_logo.setImageDrawable(data.loadIcon(packageManager));
// add_app.setText(appDetailsList.get(position).name);
checkBox.setText(data.loadLabel(packageManager));
}
//
// view.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
//
// Toast.makeText(context, "Clicked JIMMY", Toast.LENGTH_SHORT).show();
//
// if (x == 4 && !add_app.isChecked()) {
// Toast.makeText(context, "You reached your limit", Toast.LENGTH_SHORT).show();
// add_app.setChecked(false);
// return;
// }
//
// if (!add_app.isChecked()) {
// x++;
// add_app.setChecked(true);
// return;
// }
//
// if (add_app.isChecked()) {
// x--;
// add_app.setChecked(false);
// return;
// }
//
// }
//
// });
return view;
}
}
The Activity
public class SelectAppsActivity extends ListActivity {
private PackageManager packageManager = null;
private List<ApplicationInfo> applist = null;
private ListofAppsAdapter listofAppsAdapter = null;
RelativeLayout goBackappsHolder, proceedappsHolder;
ListView listsofappsrecyclerview;
CharSequence packageName, app_Name;
Drawable icon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_apps);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setNavigationBarColor(getResources().getColor(R.color.darkTheme));
}
goBackappsHolder = findViewById(R.id.goBackappsHolder);
proceedappsHolder = findViewById(R.id.proceedappsHolder);
packageManager = getPackageManager();
new loadApps().execute();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
try {
} catch (ActivityNotFoundException e) {
Toast.makeText(SelectAppsActivity.this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(SelectAppsActivity.this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> installedApplications) {
ArrayList<ApplicationInfo> appList = new ArrayList<ApplicationInfo>();
for (ApplicationInfo info : installedApplications) {
try {
if (packageManager.getLaunchIntentForPackage(info.packageName) != null) {
appList.add(info);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return appList;
}
private class loadApps extends AsyncTask<Void, Void, Void> {
private ProgressDialog progressDialog = null;
@Override
protected Void doInBackground(Void... voids) {
applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
listofAppsAdapter = new ListofAppsAdapter(SelectAppsActivity.this, R.layout.listsofapps_layout, applist);
return null;
}
@Override
protected void onPostExecute(Void unused) {
setListAdapter(listofAppsAdapter);
progressDialog.dismiss();
super.onPostExecute(unused);
}
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(SelectAppsActivity.this, null, "Loading app info...");
super.onPreExecute();
}
}
}
I now known how to solve it. Override the following methods.
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}