OnCheckedChanged fired several times, Listview with checkbox

I have a ListView with a checkbox:

  • Oncheckedchanged(..) is called when the user checks / deselects the listview listview
  • Oncheckedchanged(..) is called again when the user clicks listitem via onItemClick(.....)

Is this a known issue? how to differentiate events.

 public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (convertView == null) { LayoutInflater inflator = context.getLayoutInflater(); convertView = inflator.inflate(R.layout.row, null); viewHolder = new ViewHolder(); viewHolder.text = (TextView) convertView.findViewById(R.id.label); viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check); viewHolder.imageview= (ImageView) convertView.findViewById(R.id.imageView1); viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag. list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state. } }); convertView.setTag(viewHolder); convertView.setTag(R.id.label, viewHolder.text); convertView.setTag(R.id.check, viewHolder.checkbox); convertView.setTag(R.id.imageView1, viewHolder.imageview); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.checkbox.setTag(position); // This line is important. viewHolder.imageview.setTag(position); viewHolder.text.setText(list.get(position).getName()); viewHolder.checkbox.setChecked(list.get(position).isSelected()); // change the default-image here if(list.get(position).getcontact_id()==5) { viewHolder.imageview.setImageResource(android.R.drawable.ic_dialog_map); }... .. return convertView; } 

EDIT

onCheckedChanged () is called

  • When no item is selected and the user clicks on it
  • If the list consists of 3 elements and when the user clicks on any other elements, onItemclick () is called, and onCheckedChanged () is launched 3 times (= no: elements in the list)
+8
android android-listview listview
source share
2 answers

Expected Behavior:

  • onCheckedChanged(CompoundButton buttonView, boolean isChecked) is called for each item when they are checked / not checked. Android has decided to track all the statuses of goods for you and calls you for each item every time it has been changed. Using the isChecked parameter isChecked you can distinguish what happened.

  • onItemClick() is called whenever one of the elements that was clicked is not necessarily a flag in the element, but somewhere. Typically, an item is subsequently selected — again, not always.

  • If you need to know which item was actually selected from the list, use OnItemSelectedListener.onItemSelected() . This is the one that is called to get the selection (the whole element).

BTW: you do not need to manually specify the flag behavior. Checking / unchecking and ticking the box is performed by Android. You just need to get the verification status as soon as you know which one is selected. Thus, the onCheckedChanged implementation onCheckedChanged not needed at all, as far as I can see.

+6
source share

Replace onCheckChangeListener with onClickListener.

CheckChanged will be called twice since it will be called when you call the setChecked () method, and when you click on this check box.

+13
source share

All Articles