Custom adapters and check boxes in android listview

Source: Internet
Author: User

Custom adapters and check boxes in android listview

The following code reports an 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 normally. The position is correctly added to my ArrayList, but if (assuming that the project I selected is in position 1) I scroll the listview to the bottom and then roll back, the selection is canceled ....

 

Solution

 

Delete the code viewHolder. checkBox. setTag (position );
Put viewHolder. checkBox. getTag (); In onCheckedChanged. Follow the code below to modify

 

@Overridepublic 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);            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;}}


 

You can delete viewHolder. checkBox. setTag (position); to recycle the ListView, you need to report the error location in the tag and get the onCheckedChanged

The same is true for adding viewHolder. checkBox. getTag ().

 

 

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.