I did listview with set of rows. Each rows having a checkbox. If I am clicking one checkbox, another checkbox also checked. What I will do to avoid this ? Anyone knows help me..
Asked
Active
Viewed 2,706 times
3 Answers
2
To avoid the problems with CheckBoxes in a ListView you can take a Boolean array initialized false in the beginning and then making true the corresponding position in the array where checkbox is checked in the ListView. This will not cause a trouble in checked state of checkboxes when you move forward and backward in your application or scroll the ListView.
Here is how to set checkboxstate is the boolean array:
holder.checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (((CheckBox) v).isChecked())
checkBoxState[pos] = true;
else
checkBoxState[pos] = false;
}
});
and then this will keep the checkboxes checked when you scroll and the state will not automatically change:
holder.checkbox.setChecked(checkBoxState[pos]);

Rishabh Srivastava
- 3,683
- 2
- 30
- 58
-
use it in your getView() method.If you don't know the use of holder then please study your Custom ListView thoroughly. – Rishabh Srivastava Sep 10 '13 at 05:12
-
public View getView(int position, View convertView, ViewGroup parent) { View vi=convertView; vi = inflater.inflate(R.layout.mainlist_item,null); ImageView imgview=(ImageView)vi.findViewById(R.id.list_image); TextView artist = (TextView)vi.findViewById(R.id.nameid); TextView view = (TextView)vi.findViewById(R.id.deptid); imgview.setImageBitmap(imgs.get(position)); artist.setText(empname.get(position)); view.setText(empdept.get(position)); return vi; } – sundaramoorthy Sep 10 '13 at 05:40
-
This is my code.. Its working for single checked. But unchecked while scrolling up and down.. – sundaramoorthy Sep 10 '13 at 05:41
-
please post the code in proper format....is not readable plus post your logcat....I am posting my code...understand it. – Rishabh Srivastava Sep 11 '13 at 12:52
1
This is my code:
public class Favourites extends Activity {
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listfavourites);
initcomponents();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});
ArrayList<HashMap<String, String>> alist = new ArrayList<HashMap<String, String>>();
for (int i = 1; i < 20; i++) {
HashMap<String, String> hmap = new HashMap<String, String>();
hmap.put("itemname", "ItemName" + i);
hmap.put("price", "Price");
alist.add(hmap);
}
final CustomListAdapter adapter = new CustomListAdapter(this,
R.layout.listitemfavourites, alist);
list.setAdapter(adapter);
}
private void initcomponents() {
list = (ListView) findViewById(R.id.listfavourites_lst_list);
}
class CustomListAdapter extends ArrayAdapter<HashMap<String, String>> {
Context context;
boolean[] checkBoxState;
int textViewResourceId;
ArrayList<HashMap<String, String>> alist;
public CustomListAdapter(Context context, int textViewResourceId,
ArrayList<HashMap<String, String>> alist) {
super(context, textViewResourceId);
this.context = context;
this.alist = alist;
this.textViewResourceId = textViewResourceId;
checkBoxState = new boolean[alist.size()];
}
public int getCount() {
return alist.size();
}
public View getView(final int pos, View convertView, ViewGroup parent) {
Holder holder = null;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.listitemfavourites, parent,
false);
holder = new Holder();
holder.checkbox = (CheckBox) convertView
.findViewById(R.id.listitemfavourites_chk_checkbox);
holder.itemname = (TextView) convertView
.findViewById(R.id.listitemfavourites_txt_itemname);
holder.price = (TextView) convertView
.findViewById(R.id.listitemfavourites_txt_price);
holder.lin_background = (LinearLayout) convertView
.findViewById(R.id.favourites_lin_top);
convertView.setTag(holder);
holder = (Holder) convertView.getTag();
holder.itemname.setText(alist.get(pos).get("itemname"));
holder.price.setText(alist.get(pos).get("price"));
holder.checkbox.setChecked(checkBoxState[pos]);
if (pos == 0) {
holder.lin_background
.setBackgroundResource(R.drawable.bg_celltop);
} else if (pos == (alist.size() - 1) && alist.size() != 1) {
holder.lin_background
.setBackgroundResource(R.drawable.bg_cellbottom);
}
holder.checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (((CheckBox) v).isChecked())
checkBoxState[pos] = true;
else
checkBoxState[pos] = false;
}
});
return convertView;
}
class Holder {
TextView itemname, price;
CheckBox checkbox;
LinearLayout lin_background;
}
}
}

Rishabh Srivastava
- 3,683
- 2
- 30
- 58
0
This is because you are recycling the view by the condition if(view==null) remove this.

Rishabh Srivastava
- 3,683
- 2
- 30
- 58
-
Its working.. But If I am scrolling the listview means, the checkbox will unchecked automatically – sundaramoorthy Sep 07 '13 at 12:36