Android automatically calls onCheckedChanged when the ListView is rolling, causing constant changes in the CheckBox status. androidlistview

Source: Internet
Author: User

Android automatically calls onCheckedChanged when the ListView is rolling, causing constant changes in the CheckBox status. androidlistview

When creating a ListView with a CheckBox, we found that when the status of the CheckBox is initialized, The ListView is rolled, and the selected status of the CheckBox continuously changes. The final cause is that the ListView automatically calls onCheckedChanged when scrolling. After checking the solutions for various blogs, there is a solution on a foreign website to solve my problem. Write it down and share it with you.

 

Write this in the getView method of the custom Adapter.

 

 

Java code
  1. // Before initializing the CheckBox status and setting the status change listener event, set the status change listener event to null.
  2. Holder. checkBox. setOnCheckedChangeListener (null );
  3. // Then set the CheckBox status
  4. If (isChecked ){
  5. Holder. checkbox. setChecked (true );
  6. } Else {
  7. Holder. checkbox. setChecked (false );
  8. }
  9. // Then set the status change listener event
  10. Holder. checkBox. setOnCheckedChangeListener (new CompoundButton. OnCheckedChangeListener (){
  11. @ Override
  12. Public void onCheckedChanged (CompoundButton buttonView, boolean isChecked ){
  13. If (isChecked ){
  14. //...
  15. } Else {
  16. //....
  17. }
  18. }
  19. });

 

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.