In Android, listview uses checkbox to determine the selected item

Source: Internet
Author: User

In this article, you can add the checkbox control to the custom listview layout and perform operations on the selected items of the listview by determining whether or not to select the checkbox. This function is displayed in a demo. Select an item in listview and click the button to display the items selected.

[1] program structure:

Specifically, person. Java is an entity class, while mainactivity. Java is an activity component class. Listitem. XML is a custom list of layout files for each item.

[2] the source code of the listitem. xml layout file is as follows:

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent">  <LinearLayout     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:orientation="horizontal"     android:descendantFocusability="blocksDescendants">  <CheckBox     android:id="@+id/list.select"     android:layout_width="wrap_content"     android:layout_height="wrap_content"/>  <TextView     android:id="@+id/list.name"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:layout_weight="1"     android:text="Name"     android:layout_gravity="center"     android:textSize="20dp"     android:layout_marginLeft="10dp"/>  <TextView     android:id="@+id/list.address"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:layout_weight="1"     android:text="Address"     android:layout_gravity="center"     android:textSize="20dp"/>  </LinearLayout></LinearLayout>

[3] the source code of the Main. xml layout file is as follows:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <Button       android:id="@+id/show"       android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:text="Show"/>    <ListView       android:id="@+id/lvperson"       android:layout_width="fill_parent"       android:layout_height="fill_parent"/></LinearLayout>

[4] the source code of the person. Java object class is as follows:

package com.andyidea.bean;public class Person {private String name;private String address;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}}

[5] mainactivity. Java class source code is as follows:

Package COM. andyidea. listview; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import COM. andyidea. bean. person; import android. app. activity; import android. app. alertdialog; import android. content. context; import android. OS. bundle; import android. util. log; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup; import android. widget. baseadapter; import android. widget. button; import android. widget. checkbox; import android. widget. listview; import android. widget. textview; public class mainactivity extends activity {button show; listview lv; List <person> Persons = new arraylist <person> (); Context mcontext; mylistadapter adapter; list <integer> listitemid = new arraylist <integer> ();/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); mcontext = getapplicationcontext (); show = (button) findviewbyid (R. id. show); Lv = (listview) findviewbyid (R. id. lvperson); initpersondata (); adapter = new mylistadapter (persons); LV. setadapter (adapter); show. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {listitemid. clear (); For (INT I = 0; I <adapter. mchecked. size (); I ++) {If (adapter. mchecked. get (I) {listitemid. add (I) ;}} if (listitemid. size () = 0) {alertdialog. builder builder1 = new alertdialog. builder (mainactivity. this); builder1.setmessage ("no record selected"); builder1.show ();} else {stringbuilder sb = new stringbuilder (); For (INT I = 0; I <listitemid. size (); I ++) {sb. append ("Itemid =" + listitemid. get (I) + ". ");} alertdialog. builder builder2 = new alertdialog. builder (mainactivity. this); builder2.setmessage (sb. tostring (); builder2.show () ;}});}/*** simulate data */private void initpersondata () {person mperson; For (INT I = 1; I <= 12; I ++) {mperson = new person (); mperson. setname ("Andy" + I); mperson. setaddress ("Guangzhou" + I); persons. add (mperson) ;}}// custom listview adapter class mylistadapter extends baseadapter {list <Boolean> mchecked; List <person> listperson; hashmap <integer, view> map = new hashmap <integer, View> (); Public mylistadapter (list <person> List) {listperson = new arraylist <person> (); listperson = List; mchecked = new arraylist <Boolean> (); For (INT I = 0; I <list. size (); I ++) {mchecked. add (false) ;}@ overridepublic int getcount () {return listperson. size () ;}@ overridepublic object getitem (INT position) {return listperson. get (position) ;}@ overridepublic long getitemid (INT position) {return position ;}@ overridepublic view getview (INT position, view convertview, viewgroup parent) {view; viewholder holder = NULL; If (map. get (position) = NULL) {log. E ("mainactivity", "position1 =" + position); layoutinflater minflater = (layoutinflater) mcontext. getsystemservice (context. layout_inflater_service); view = minflater. inflate (R. layout. listitem, null); holder = new viewholder (); holder. selected = (checkbox) view. findviewbyid (R. id. list_select); holder. name = (textview) view. findviewbyid (R. id. list_name); holder. address = (textview) view. findviewbyid (R. id. list_address); Final int P = position; map. put (Position, view); holder. selected. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {checkbox cb = (checkbox) V; mchecked. set (p, CB. ischecked () ;}}); view. settag (holder);} else {log. E ("mainactivity", "position2 =" + position); view = map. get (position); holder = (viewholder) view. gettag ();} holder. selected. setchecked (mchecked. get (position); holder. name. settext (listperson. get (position ). getname (); holder. address. settext (listperson. get (position ). getaddress (); Return view ;}} static class viewholder {checkbox selected; textview name; textview address ;}}

[6] after the program runs, the result is as follows:

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.