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