Checkbox status change listener caused by view recycle in listview

Source: Internet
Author: User

Previously, when the custom adapter is passed to the listview, because the view of the listview is recycled, you must note that when the listview list item contains a status control. Details can be seen before post [url = custom adapter implementation listview with multi-choice box and other status controls note http://www.eoeandroid.com/thread-248749-1-1.html
Let's talk about this problem because there are two linesCodeProblem caused by the opposite position.

Each view in my listview contains an imageview, textview, and checkbox. If one or more checkboxes in the listview are selected, the buttons on the listview are displayed. Otherwise, the buttons are hidden. Therefore, you need to set a listener for the checkbox in each view line. I use the oncheckedchangelistener listener in the checkbox. This listener is triggered when the checkbox status changes. First, let's take a look at some key code in the getview method of the adapter that I customized to listview:
This is the internal class used in the getview method:

Static ClassViewholder {PublicImageview;PublicTextview;PublicCheckbox ;}

This is the code that uses the listview recycling mechanism to reuse the view cyclically in the getview method:

                 Public View getview ( Int  Position, view convertview, viewgroup parent) {viewholder;  If (Convertview =Null  ) {Convertview = Inflater. Inflate (R. layout. searchitem, Null  ); Viewholder = New  Viewholder (); viewholder. imageview = (Imageview) convertview. findviewbyid (R. Id. searchitemimage); viewholder. textview = (Textview) convertview. findviewbyid (R. Id. searchitemtext); viewholder. checkbox = (Checkbox) convertview. findviewbyid (R. Id. searchitemcheckbox); convertview. settag (viewholder );}  Else  {  //  Log. I (codeutils. searchtag, "view is reuse "); Viewholder = (Viewholder) convertview. gettag ();} 

The following is the code for setting the display status and listener for the checkbox:

Viewholder. checkbox. setoncheckedchangelistener (NewSearchitemoncheckedchangelistener (Position, State); viewholder. checkbox. setchecked (State [position]);

As mentioned before, because the collection of listview requires an array or list to record the status of checkbox in each data item. Here, state is a Boolean array with a length such as the listview list. It is used to record the status that checkbox should display on the data identified by each position (that is, the ID of each list item data, the initial status is false. When constructing a checkbox listener, you must pass the position of the current view and the state of the Status array of the entire checkbox list. The following code changes the listener in the checkbox status:

                 Public   Class Searchitemoncheckedchangelistener Implements Oncheckedchangelistener {  Private   Int  ID;  Private  Boolean [] State;  Public Searchitemoncheckedchangelistener ( Int  ID, Boolean [] State ){  This . ID = ID;  This . State =State;} @ override  Public   Void  Oncheckedchanged (compoundbutton buttonview,  Boolean  Ischecked) {State [ID] = Ischecked;  If  (Ischecked) {checkedcount ++ ;}  Else {Checkedcount -- ;}  If (Checkcoutn> 0 ) {Searchbutton. setvisibility (button. Invisible );}  Else  {Searchbutton. setvisibility (button. Visible );}}}} 

Here, an integer whose initial checkedcount value is 0 is used to record the number of selected multiple boxes. The searchbutton determines whether to display or hide the button Based on the checkbox.

The implementation code of the above logic function. At the beginning, this is a problem that is caused by the reverse position of the listview collection mechanism and the two lines of code. The opposite of the two lines of code will lead to completely different results. It refers to the two lines of code that set the checkbox listener and status. At first, my order is:

Viewholder. checkbox. setchecked (State [position]); viewholder. checkbox. setoncheckedchangelistener (NewSearchitemoncheckedchangelistener (Position, State ));

The problem with this sequence is that after I pull the list, the status of the list items whose pull is hidden will be changed to false. This is incredible, because I have separated an array of States to record the status of each checkbox. There is only one possibility, that is, the value in the array of States has changed, the Value Location of the change status array lies in oncheckedchangelistener. After debugging for several hours, I figured out that the problem was that the two lines of code were in order of location.

The reason is that we have to talk about the recycling mechanism of listview. If my listview can display up to 10 views, it will call getview ten times at first to construct ten completely new views (including setting listeners for the checkbox ). When I pull down the list to display 11th list items, the first list item on the top is hidden and will call getview again, however, in this case, the getview parameter returns the view of the first hidden list item, and changes the data of this view as the 11th views to be displayed. The problem lies here. I put the checkbox. setchecked () method call before setting the listener. At this time, because the checkbox status is changed, it is bound to cause the listener that triggers the status change. Note! Because 11th views are recycled from the hidden 1st views, although the Code for setting the listener in the next row has not been executed, it actually has a status listener, set whether the listener is the view or the first view. At that time, the listener set the first item of data changed, instead of 11th items of data. Therefore, 11th items of data cannot be correctly modified, but 1st items of innocent data are modified. If I reverse the two lines of code, first change the listener, and then set the status, the listener is naturally a new listener, and the logic is correct.

 

Original article: http://www.eoeandroid.com/thread-249189-1-1.html

 

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.