I have made a listview with custom adapter. It has an Imageview, textview and a checkbox
---------------------------------------
<ImageView --- textView --- checkbox >
---------------------------------------
I am using a SparseBooleanArray named "mCheckStates" to keep a track of the states of the checkboxes. Also I want to restrict users to check only two checkboxes at a time. for this I have used an int variable as a counter.
But when I scroll the listView onCheckChangedListener is being called and the count is reduced by one and sometimes by two, allowing user to check 3 or 4 checkboxes.
How can I stop this from happening? Please help me find a solution. Thanks.
Code
protected class MyCustomAdapter extends ArrayAdapter<String> implements
CompoundButton.OnCheckedChangeListener {
ArrayList<String> myList;
int count = 0;
ViewHolder holder = null;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<String> sList) {
super(context, textViewResourceId, sList);
mCheckStates = new SparseBooleanArray(sList.size());
myList = sList;
}
private SparseBooleanArray mCheckStates;
private class ViewHolder {
TextView text;
CheckBox chkbox;
ImageView imageview;
}
@Override
public int getCount() {
return stg1.size();
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.new_search_adptr, null);
holder = new ViewHolder();
holder.text = (TextView) convertView
.findViewById(R.id.adapterText1);
holder.chkbox = (CheckBox) convertView
.findViewById(R.id.checkBox1);
holder.imageview = (ImageView) convertView
.findViewById(R.id.imageView1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.chkbox.setTag(position);
holder.chkbox.setChecked(mCheckStates.get(position, false));
holder.chkbox.setOnCheckedChangeListener(this);
holder.text.setText(stg1.get(position).toString());
holder.imageview.setImageBitmap(arr_img.get(position));
return convertView;
}
public boolean isChecked(int position) {
System.out.println("inside isChecked");
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
System.out.println("inside setChecked");
mCheckStates.put(position, isChecked);
}
public void toggle(int position) {
System.out.println("inside toggle");
setChecked(position, !isChecked(position));
}
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
System.out.println("\n inside onCheckedChanged: " + isChecked);
if (isChecked) {
System.out.println("inside isChecked true");
System.out.println("count= " + count);
if (count >= 2) {
// count--;
System.out.println("inside count>2: " + count);
buttonView.setChecked(false);
Toast.makeText(getApplicationContext(),
"Please select only two cars!", Toast.LENGTH_LONG)
.show();
} else {
count++;
System.out.println("inside else: " + count);
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
} else {
System.out.println("inside isChecked false" + count);
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
count--;
}
}
}