Set multiple items for listview

Source: Internet
Author: User


First, define a class to associate the adapter and activity to remember the click position.

public class MyViewHoler{public CheckBox cb;public TextView tv;}


Ii. Adapter writing

Package COM. example. listviewcheckdemo; import Java. util. hashmap; import Java. util. list; import android. annotation. suppresslint; 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. textview;/***** @ author liuziwei * select list adapter **/public class checkadapter extends baseadapter {public static hashmap <integer, Boolean> isselected; private context = NULL; private list <string> List = NULL; @ suppresslint ("usesparsearrays") Public checkadapter (context, list <string> List) {This. context = context; this. list = List; isselected = new hashmap <integer, Boolean> (); For (INT I = 0; I <list. size (); I ++) {isselected. put (I, false) ;}@overridepublic int getcount () {return list. size () ;}@ overridepublic object getitem (INT arg0) {return list. get (arg0) ;}@ overridepublic long getitemid (INT arg0) {return 0 ;}@ overridepublic view getview (INT position, view, viewgroup arg2) {myviewholer holder = NULL; if (view = NULL) {holder = new myviewholer (); view = layoutinflater. from (context ). inflate (R. layout. check_item, null); holder. TV = (textview) view. findviewbyid (R. id. item_ TV); holder. CB = (checkbox) view. findviewbyid (R. id. item_cb); view. settag (holder);} else {holder = (myviewholer) view. gettag ();} holder. TV. settext (list. get (position); holder. CB. setchecked (isselected. get (position); Return view ;}}


3. Writing in Activity


Public class mainactivity extends activity {private listview listview_check; private checkadapter adapter;/** initialization data */private list <string> newlist = new arraylist <string> (); /** store the selected data */private list <string> checklist = new arraylist <string> ();/** select all */private button btn_all; /** invert */private button btn_clear;/** deselect selected */private button btn_cancel; @ overrideprotected void oncreate (bundle savedinstancestate) {s Uper. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); listview_check = (listview) findviewbyid (R. id. listview); btn_all = (button) findviewbyid (R. id. all); btn_clear = (button) findviewbyid (R. id. clear); btn_cancel = (button) findviewbyid (R. id. cancel);/** initialization data */For (INT I = 0; I <10; I ++) {newlist. add ("option" + I);} adapter = new checkadapter (this, newlist); listview_check.setadapter (adapter); listview_check. Setonitemclicklistener (New onitemclicklistener () {@ overridepublic void onitemclick (adapterview <?> Arg0, view, int position, long arg3) {// todo auto-generated method stubmyviewholer holder = (myviewholer) view. gettag (); holder. CB. toggle (); // Changes the checkbox status checkadapter every time you obtain the clicked item. isselected. put (Position, Holder. CB. ischecked (); // Changes the Save status of MAP values at the same time if (holder. CB. ischecked () = true) {checklist. add (newlist. get (position);} else {checklist. remove (newlist. get (position);} log. E ("----", "---------" + checklist. tostring () ;}});/** select all */btn_all.setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view arg0) {checklist. clear (); For (INT I = 0; I <newlist. size (); I ++) {checkadapter. isselected. put (I, true); checklist. add (newlist. get (I);} adapter. notifydatasetchanged (); // note that this sentence must be added; otherwise, the checkbox cannot be updated normally});/** reselect */btn_clear.setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view arg0) {for (INT I = 0; I <newlist. size (); I ++) {If (checkadapter. isselected. get (I) = false) {checkadapter. isselected. put (I, true); checklist. add (newlist. get (I);} else {checkadapter. isselected. put (I, false); checklist. remove (newlist. get (I) ;}} adapter. notifydatasetchanged () ;}});/** cancel selected */btn_cancel.setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view arg0) {for (INT I = 0; I <newlist. size (); I ++) {If (checkadapter. isselected. get (I) = true) {checkadapter. isselected. put (I, false); checklist. remove (newlist. get (I) ;}} adapter. notifydatasetchanged ();}});}







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.