Problems encountered when developing listview with checkbox or button in Android

Source: Internet
Author: User
Android development often encounters checkbox and button in listview. Here there are three main problems: 1. when the listview contains a checkbox or button, the listview item cannot respond to the click event. This is mainly because the focus retrieval capability of the checkbox or button is higher than that of the listview, so that its subcontrol cannot obtain the focus, that is, set the sub-control attribute Android: focusable = "false" in the layout file or add the Android: descendantfocusability = "blocksdescendants" attribute to the layout of the listview adapter, descendantfocusability indicates that all child controls lose focus. 2. when the listview contains a checkbox, when the checkbox is selected, sliding the listview will find multiple selected checkboxes, that is, there is a dislocation. This is mainly related to the getview method mechanism of the adapter, that is, each page of the listview displays a fixed number of items. When sliding, the items on the previous screen are only re-drawn, first, you can learn about the refresh mechanism of the getview of the custom adpter. The solution to this problem is to record the position of the selected checkbox (this method is the prerequisite for selecting the checkbox when you click the item of the listview ), in getview, the checkbox status is set based on the selected position. For specific code, see private list <string> selectposition = newarraylist <string> ();
PrivateClass listitems {
Checkbox itemchechbox;
Textview itemname ;}@ override public view getview (int
Position, viewconvertview, viewgroup parent) {listitemsitems =
NULL; If (convertview =
Null) {items =
New listitems (); convertview =
Minflater. Inflate (R. layout. Upload_from_file_browser_item,
Null); items. itemchechbox = (checkbox) convertview. findviewbyid (R. Id. Cb_choose_file); Items. itemname = (textview) convertview. findviewbyid (R. Id. TV _upload_item); Convertview. settag (items );}
Else {items = (listitems) convertview. gettag ();} mlistview. setonitemclicklistener (newonitemclicklistener ()
{@ Override publicvoidonitemclick (adapterview <?> Arg0, View
View, int position,
Long ID) {listitemsitem = (listitems) view. gettag (); If (selectposition. Contains (""
+ Position )){
Item. itemchechbox. setchecked (false );
Selectposition. Remove ("" + position);} else {selectposition. Add (""
+ Position );

Item. itemchechbox. setchecked (true); // records the position of the selected item


}); If (selectposition. Contains (""
+ Position) {// display the selected checkbox items. itemchechbox. setchecked (true );}
Else {items. itemchechbox. setchecked (false) ;}} return convertview;} 3. when a button is contained in a listview, we sometimes need to know which position the button is located under, that is, to obtain the item data. Because the Click Event of a button is asynchronous, the Click location cannot be obtained. This is similar to the checkbox above. In this case, you can set a tag for the button and obtain the tag to determine the location when you click the button. The implementation code is as follows: @ override public view getview (int
Position, viewconvertview, viewgroup parent) {listholderholder; If (convertview =
Null) {holder = new listholder (); convertview =
Minflater. Inflate (R. layout.Homework_list_item,
Null); Holder. homeworkupload = (button) convertview. findviewbyid (R. Id.Homework_list_btn_uploadhomework); Convertview. settag (holder);} else {holder = (listholder) convertview. gettag ();} holder. homeworkupload. settag (position); Holder. homeworkupload. setonclicklistener (New
Onclicklistener () {@ override
Publicvoid onclick (view v ){/***
The upload task function is temporarily blocked */log.I("Hubei ",
"Click position:" + v. gettag (); object homework =
List. Get (integer.Parseint("" + V. gettag () ;}) ;}return convertview ;}

Related Article

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.