When I wrote the code the other day, I added a checkbox to the ListView, but I didn't think there was any problem with the logic, but when we tested it, we found that the status of the checkbox was chaotic.
Like the 1, 2 I chose here.
When I come back in, it becomes
</pre><p></p><p></p><p> at first did not know why, later on the internet after a large pile of search, There are some problems in discovering the original logic. </p><p></p><p> in this code, </p><p></p><pre name= "Code" class= "Java" > @Overridepublic view getView (int position, view Convertview, ViewGroup parent) {//TODO Auto-gener Ated method stubfinal DeviceInfo item = items.get (position); Viewholder holder=null;if (convertview==null) {holder=new viewholder (); Convertview = Inflater.inflate (R.layout.lv_ Item_device, NULL); Holder.tvdevicetype= (TextView) Convertview.findviewbyid (R.id.tv_device_type); holder.tvdeviceno= (TextView) Convertview.findviewbyid (r.id.tv_device_no); holder.cbischeck= (CheckBox) Convertview.findviewbyid (R.id.cb_device_ischeck); Convertview.settag (holder);} Else{holder= (Viewholder) Convertview.gettag ();} Holder.tvDeviceType.setText (Item.getdevicecategory () + ""); Holder.tvDeviceNo.setText (Item.getdevicenum ()); Holder.cbIscheck.setChecked (Item.isselcet ());Er.cbIscheck.setOnCheckedChangeListener (New Oncheckedchangelistener () {@Overridepublic void oncheckedchanged ( Compoundbutton Buttonview, Boolean isChecked) {//TODO auto-generated method Stubitem.setselcet (isChecked);}}); return Convertview;}
Before assigning a value, and then setting the listener, a problem arises, such as assigning the first assignment, when its item is assigned, and the holder does not have a listener, so the original list is not triggered. So the data in the list is probably due to the fact that this code is not valid:
Holder.cbIscheck.setOnCheckedChangeListener (New Oncheckedchangelistener () {@Overridepublic void oncheckedchanged ( Compoundbutton Buttonview, Boolean isChecked) {//TODO auto-generated method Stubitem.setselcet (isChecked);}});
and not consistent.
The solution is simple, put the assignment behind the listener
As follows:
Public View GetView (int position, View Convertview, ViewGroup parent) {//TODO auto-generated method Stubfinal deviceinfo item = items.get (position); Viewholder holder=null;if (convertview==null) {holder=new viewholder (); Convertview = Inflater.inflate (R.layout.lv_ Item_device, NULL); Holder.tvdevicetype= (TextView) Convertview.findviewbyid (R.id.tv_device_type); holder.tvdeviceno= (TextView) Convertview.findviewbyid (r.id.tv_device_no); holder.cbischeck= (CheckBox) Convertview.findviewbyid (R.id.cb_device_ischeck); Convertview.settag (holder);} Else{holder= (Viewholder) Convertview.gettag ();} Holder.cbIscheck.setOnCheckedChangeListener (New Oncheckedchangelistener () {@Overridepublic void oncheckedchanged ( Compoundbutton Buttonview, Boolean isChecked) {//TODO auto-generated method Stubitem.setselcet (isChecked);}}); Holder.tvDeviceType.setText (Item.getdevicecategory () + ""); Holder.tvDeviceNo.setText (Item.getdevicenum ()); Holder.cbIscheck.setChecked (Item.isselcet ()); return Convertview;}
The problem is solved, in a word, do the listener first, then assign value.
Reasons for the clutter of ListView data in Android and how to fix it