Customize adapters and check boxes in Android ListView

Source: Internet
Author: User

The following code is the error, the code is as follows

@Overrideppublic View GetView (final int position, final View Convertview, final ViewGroup parent) {Final Viewholder      Viewholder;      View view = Convertview;          if (view = = null) {view = Layoutinflater.from (Mcontext). Inflate (R.layout.row, parent, false);           Viewholder = new Viewholder ();          Viewholder.texttitle = (TextView) View.findviewbyid (r.id.title);           Viewholder.checkbox = (CheckBox) View.findviewbyid (R.id.checkbox);          ViewHolder.checkBox.setTag (position);           View.settag (Viewholder);      Viewholder.imageview = (ImageView) View.findviewbyid (R.id.activity_googlecards_card_imageview);          } else {Viewholder = (Viewholder) view.gettag ();      ViewHolder.checkBox.getTag (position);       } viewHolder.textTitle.setText (GetItem (position). GetTitle ());      ViewHolder.checkBox.setChecked (Myintegerarraylist.contains (position)); ViewHolder.checkBox.setOnCheckedChangeListener (New Compoundbutton.oncheckedchangelIstener () {@Override public void oncheckedchanged (Compoundbutton buttonview, Boolean isChecked) {              if (isChecked) {myintegerarraylist.add (position);              } else {Myintegerarraylist.remove ((Object) position);       }          }      });    return view;}}      private static class Viewholder {TextView texttitle; checkbox checkbox;}}


The check box itself works fine. The location is added correctly to my ArrayList, but if (assuming I checked the item in position 1) I scroll the ListView to the bottom and then roll back, check cancel ....

Processing methods

Remove Code viewHolder.checkBox.setTag (position);
Put the ViewHolder.checkBox.getTag () in the oncheckedchanged, you follow the following code changes

@Overridepublic View GetView (final int position, final View Convertview, final ViewGroup parent) {Final Viewholder V      Iewholder;      View view = Convertview;          if (view = = null) {view = Layoutinflater.from (Mcontext). Inflate (R.layout.row, parent, false);           Viewholder = new Viewholder ();          Viewholder.texttitle = (TextView) View.findviewbyid (r.id.title);            Viewholder.checkbox = (CheckBox) View.findviewbyid (R.id.checkbox);           View.settag (Viewholder);      Viewholder.imageview = (ImageView) View.findviewbyid (R.id.activity_googlecards_card_imageview);          } else {Viewholder = (Viewholder) view.gettag ();      ViewHolder.checkBox.getTag (position);       } viewHolder.checkBox.setTag (position);       ViewHolder.textTitle.setText (GetItem (position). GetTitle ());      ViewHolder.checkBox.setChecked (Myintegerarraylist.contains (position)); ViewHolder.checkBox.setOnCheckedChangeListener (New Compoundbutton.oncheckedchangelisteNER () {@Override public void oncheckedchanged (Compoundbutton buttonview, Boolean isChecked) {              if (isChecked) {Myintegerarraylist.add (ViewHolder.checkBox.getTag ());              } else {Myintegerarraylist.remove ((Object) ViewHolder.checkBox.getTag ());       }          }      }); return view;}}


let you erase viewHolder.checkBox.setTag (position); To recycle the ListView, you need to place an error in the tag and let OnCheckedChanged get

Adding ViewHolder.checkBox.getTag () is the same reason


Original address: http://www.itmmd.com/201411/119.html
This article by Meng Meng's IT person to organize the release, reprint must indicate the source.

Customize adapters and check boxes in Android ListView

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.