20150526
ListView is a commonly used control, often with a custom adapter, in order to improve the display efficiency, often use view reuse to prevent repainting, but because the reuse of the old view, often resulting in the data displayed due to the location of position confusion. I have encountered this problem many times in an app project, including a button can be a good solution, but today encountered in the ListView item has Togglelbutton situation, The binding listener is ToggleButton's Compoundbutton.oncheckedchangelistener (), there is a problem, has not been resolved, and finally the item's monitoring replaced by View.onclicklistener ( ) to solve the problem.
In general, in order to prevent data confusion, the Convertview will determine the null If-else after the display data in the list. Examples of GetView are as follows:
1 @Override2 PublicView GetView (intposition, View Convertview, viewgroup parent)3 {4 //TODO auto-generated Method Stub5 FinalViewholder Holder;6 //optimize listview-Eliminate reuse and prevent ToggleButton from clicking on the wrong location7 if(Convertview = =NULL)8 {9 //use a custom layoutTenHolder =NewViewholder (); OneConvertview =Minflater A. Inflate (R.layout.list_invite_party_member,NULL); - //initializing elements in a layout -Holder.ivavatar =(ImageView) Convertview.findviewbyid (r.id.iv_avater); theHolder.tvname =(TextView) Convertview.findviewbyid (r.id.tv_name); -Holder.tvtag =(TextView) Convertview.findviewbyid (R.id.tv_tag); -Holder.btnselect =(ToggleButton) Convertview - . Findviewbyid (r.id.btn_select); +Holder.linearlayout =(linearlayout) Convertview - . Findviewbyid (R.id.rl_friend_item); + Convertview.settag (holder); A}Else at { -Holder =(Viewholder) Convertview.gettag (); - } - - //binding Data - Final intindex =position; inUserBean Bean =Listfriend.get (index); - //Set Avatar to if(!Textutils.isempty (Bean.getuseravatar ())) + { -String Avatarurl = Constant.url_user_avater +Bean.getuseravatar (); the //initializing an asynchronous load Avatar object *Finalbitmap =finalbitmap.create (context); $ finalbitmap.configloadingimage (r.drawable.user_head_02);Panax Notoginseng Finalbitmap.display (Holder.ivavatar, avatarurl); -}Else the { + Holder.ivAvatar.setImageResource (r.drawable.user_head_02); A } the + if(!Textutils.isempty (Bean.getusernickname ())) - { $ Holder.tvName.setText (Bean.getusernickname ()); $}Else - { - Holder.tvName.setText (Bean.getuserphone ()); the } - if(!textutils.isempty (Commonutils.getusertags (Bean)))Wuyi { the Holder.tvTag.setText (Commonutils.getusertags (Bean)); - holder.tvTag.setVisibility (view.visible); Wu}Else - { About holder.tvTag.setVisibility (view.gone); $ } - if(Textutils.equals (BEAN.GETRESERVED01 (), "1")) - { - //added occasions are displayed as deleted AHolder.btnSelect.setChecked (true); +}Else the { -Holder.btnSelect.setChecked (false); $ } theHolder.btnSelect.setOnClickListener (NewView.onclicklistener () { the the @Override the Public voidOnClick (View v) { - //TODO auto-generated Method Stub inToggleButton view =(ToggleButton) v; the //Boolean ischeckedold = View.gettext (). toString (). Equals ("Add")? True:false; the //get the latest post-click Check Status About BooleanisChecked =view.ischecked (); the if(isChecked) the { the //the person was added, button displays the delete +View.setchecked (true); -Listfriend.get (Index). SETRESERVED01 ("1"); the Bayi}Else the { the //turned out to be selected, click after the person is deleted - //Delete the person, the button displays the Add -View.setchecked (false); theListfriend.get (Index). SETRESERVED01 ("0"); the } the } the }); - /*Holder.btnselect the . Setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () the { the 94 @Override the Public void OnCheckedChanged (Compoundbutton buttonview, the boolean isChecked) the {98 About LOGUTIL.D ("ischecked=" + isChecked); - LOGUTIL.D ("index=" + index);101 //TODO auto-generated method stub102 if (isChecked)103 {104 //Added the person, button displays delete the listfriend.get (Index). SETRESERVED01 ("1");106 } else107 {108 //deleted the person, button displays the add109 listfriend.get (Index). SETRESERVED01 ("0"); the }111 } the });*/113 returnConvertview; the } the the /**117 * Elements in the layout118 */119 classViewholder - {121 ImageView Ivavatar;122 TextView Tvname;123 TextView Tvtag;124 ToggleButton Btnselect; the linearlayout LinearLayout;126}
View Code
And the final index to remember the location of the data, the following button to listen to the action of the correct data can be obtained.
But the experiment found (commented out) that using this method is still not possible, it may be the problem with Compoundbutton.oncheckedchangelistener (). Using this listener to monitor the action of the button changes the list corresponding data to cause confusion in the location. Had to borrow View.onclicklistener () to control the display of the ToggleButton. Is not the trouble, originally ToggleButton is generally the two kinds of control methods. Also note is that as long as the click of the ToggleButton, its check status will change, in View.onclicklistener () is the same.
Mark, so using adapter to remember the state of BTN this thing is still very simple, is not understanding the mechanism of compoundbutton caused by failure, fortunately there are view.onclicklistener () success precedent, Otherwise, it's too unrealistic to have a lot of list data for each view. There is also a checkbox with the item not being tried, or it should be replaced with a button or View.onclicklistener ().
Android ListView Reuse View leads to selection confusion issues