Reasons for the clutter of ListView data in Android and how to fix it

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.