Checkboxes on android listview have problems

I have a list with a custom BaseAdapter, and each row contains a checkbox and three text fields. I use Layoutinflater to inflate this line from an XML file. However, every time I check one flag, many other flags are checked in the whole list, while the original flag that I wanted to check is sometimes checked by myself, and sometimes not.

Each time the user selects a checkbox, I keep this checkbox tied to a unique value in the collection. The next time the getView method is called, I manually check / uncheck the box before returning the view inside getView () based on whether the checkbox was already in the collection or not. But, despite this, it still selects these flags, even if the test changelistener for these flags does not light up. I doubt that views are used again in getView, but don’t know what is a good way to make it all work.

+2
source share
3 answers

Avoid if (convertView == null) and another integer part This will definitely work for you. Thanks.

+6
source

the problem is definitely in your getView () method;

Try something like this

public View getView(int position, View convertView, ViewGroup parent) { View vu = convertView; ViewHolder vHolder = null; try { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); if (vu == null) { vu = (View) inflater.inflate(R.layout.list_fr_req, null); vHolder = new ViewHolder(); vHolder.checkbox = (CheckBox) vu.findViewById(R.id.my_ChkBox); vu.setTag(vHolder); } else { vHolder = (ViewHolder) vu.getTag(); } vHolder.checkbox.setOnCheckedChangeListener(this); vHolder.checkbox.setId(position); vHolder.textView.setId(position); if (myList.get(position).getCheckedStatus()) vHolder.checkbox.setChecked(true); else vHolder.checkbox.setChecked(false); } catch (Exception e) { Log.d("Exception in getview", e + ""); e.printStackTrace(); } return vu; } public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { list.get(buttonView.getId()).setCheckedStatus(true); } else { list.get(buttonView.getId()).setCheckedStatus(false); } } public static class ViewHolder { CheckBox checkbox; TextView textview; } 

Regards: N_JOY

+3
source

Use OnClickListener instead of OnCheckedChangeListener. The latter works even if you update the redesigned view to match the value of the object. In the following snippet of getView () code, I use HashMap to store the object validation value (itemMap).

  boolean checked=itemMap.get(currentObject); if(checked!=checkBox.isChecked()) checkBox.setChecked(checked); checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { CheckBox checkBox1=(CheckBox)v; if(checkBox1.isChecked()) //ckecked { itemMap.put(currentObject,true); if(checkListener!=null) checkListener.onCheck(position,convertView,parent,adapter); } else{ //unchecked itemMap.put(currentObject,false); if(checkListener!=null) checkListener.onUnCheck(position,convertView,parent,adapter); } } }); 

checkListener is an additional listener that can be added by the class user.

0
source

Source: https://habr.com/ru/post/892359/


All Articles