Recording Position Selection of listview in Android

Source: Internet
Author: User

Code directly:

<Checkbox Android: Id = "@ + ID/checkbox"Android: focusable = "false" // Android: clickable = "false" // requiredAndroid: layout_marginleft = "20dp" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/>

 

Public class listviewcheckboxactivity extends activity {private listview; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); listview = (listview) findviewbyid (R. id. listview); myadapter adapter = new myadapter (this); listview. setadapter (adapter); listview. setitemscanfocus (false); listview. setchoicemode (listview. choi Ce_mode_multiple); listview. setonitemclicklistener (New onitemclicklistener () {@ override public void onitemclick (adapterview <?> Parent, view, int position, long ID) {// todo auto-generated method stub system. out. println ("AA"); viewholder vhollder = (viewholder) view. gettag (); // Changes the checkbox status and changes the MAP value each time you obtain the clicked item. Vhollder. cbox. setchecked (true); Boolean check = vhollder. cbox. ischecked (); myadapter. isselected. Put (Position, check );}});}}

 

Package COM. leeapps.listviewcheckbox; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import Java. util. map; import android. content. context; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup; import android. widget. baseadapter; import android. widget. checkbox; import android. widget. imageview; import android. widget. textview; Public C Lass myadapter extends baseadapter {private layoutinflater minflater; private list <Map <string, Object> mdata; public static Map <integer, Boolean> isselected; Public myadapter (context) {minflater = layoutinflater. from (context); Init () ;}// initialize private void Init () {mdata = new arraylist <Map <string, Object> (); for (INT I = 0; I <25; I ++) {Map <string, Object> map = new hashmap <string, OBJ ECT> (); map. put ("title", "No." + (I + 1) + "Row title"); mdata. add (MAP);} // here, the map isselected is defined to record the status of each listitem, and the initial status is all false. Isselected = new hashmap <integer, Boolean> (); For (INT I = 0; I <mdata. size (); I ++) {isselected. put (I, false) ;}@override public int getcount () {return mdata. size () ;}@ override public object getitem (INT position) {return mdata. get (position) ;}@ override public long getitemid (INT position) {return position ;}@ override public view getview (INT position, view convertview, viewgroup parent ){ Viewholder holder = NULL; // initialize convertview when convertview is null. If (convertview = NULL) {holder = new viewholder (); convertview = minflater. inflate (R. layout. listviewcontent, null); holder. title = (textview) convertview. findviewbyid (R. id. text); holder. cbox = (checkbox) convertview. findviewbyid (R. id. checkbox); convertview. settag (holder);} else {holder = (viewholder) convertview. gettag ();} holder. title. settext (mdata. get (position ). get ("title "). tostring (); holder. cbox. setchecked (isselected. get (position); Return convertview;} public class viewholder {public textview title; Public checkbox cbox ;}}

 

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.