Combination of ListView and checkbox in Android----multiple selection and recording

Source: Internet
Author: User

Most of the time we will use the combination of the ListView and checkbox, such as a list, can be used for multiple selection, but many people seem to have encountered a lot of problems in this place, I have just started to learn the problem of false choice, when the number of the list, I tick one, Sliding page will find the entry is also checked, which is obviously not in line with our requirements, and then looked for information on the Internet, with HashMap to record a checkbox of the Tick records to solve

Here is a small demo of the implementation



This is the list of XML ListView for each item file

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical "        > <relativelayout android:id= "@+id/outpatient_check_hospital" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:layout_marginbottom= "5.0dip" android:layout_marginleft= "12 .599976dip "android:layout_marginright=" 12.599976dip "android:layout_margintop=" 5.0dip "Android:gravi Ty= "center_vertical" android:background= "#AAAAAA" > <linearlayout android:id= "@+id/linear_l Ayout_up "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "Android : layout_margin= "10.0dip" android:gravity= "center" android:orientation= "Horizontal" > & Lt;imageview Android: layout_width= "10dip" android:layout_height= "10dip" android:adjustviewbounds= "false"/>                <textview android:id= "@+id/tv_device_name" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_weight= "2.0" Android:text = "Name" android:textcolor= "#ff323232" android:textsize= "16.0SP" Android:typefac E= "monospace"/> <checkbox android:id= "@+id/checkbox1" android:layout_width                    = "Wrap_content" android:layout_height= "wrap_content" android:text= ""/> </LinearLayout> </RelativeLayout></LinearLayout>




This is the page that displays the ListView, simple initialization of several data

public class Mainactivity extends Activity {private ListView listview;private listviewadapter adapter;private string[] Be ans = new string[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "Ten", "one", "one", "a", "" "," "" "," "", "", "" verrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Initview ();} private void Initview () {//TODO auto-generated method stublog.i ("HTP", "beans.size:" + beans.length); listview = (ListView ) Findviewbyid (r.id.listview1); adapter = new Listviewadapter (mainactivity.this, beans); Listview.setadapter (adapter) ; Listview.setchoicemode (listview.choice_mode_single);}



The following is a adapter class, we still inherit the Baseadapter class. Here we use a hashmap<integer,boolean> key value to record the checkbox's selection at the corresponding position.

Package Com.example.listviewcheckboxdemo;import Java.util.hashmap;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;public Class Listviewadapter extends Baseadapter {private Context context;private string[] beans;//to control the check state of a checkbox private static Hashmap<integer, boolean> isselected;class viewholder {TextView tvname; CheckBox CB;} Public Listviewadapter (context context, string[] beans) {//TODO auto-generated constructor Stubthis.beans = beans;this.c Ontext = context;isselected = new Hashmap<integer, boolean> ();//Initialize Data initdate ();} Initialize isselected data private void Initdate () {for (int i = 0; i < beans.length; i++) {getisselected (). Put (I, false);}} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn beans.length;} @Overridepublic Object getItem (int position) {//TODO auto-generated method stubreturn beans[position];} @Overridepublic long Getitemid (int position) {//TODO auto-generated method Stubreturn position;}  @Overridepublic View GetView (final int position, view Convertview, ViewGroup parent) {//TODO auto-generated method stub// Page Viewholder holder; String bean = beans[position]; Layoutinflater Inflater = layoutinflater.from (context), if (Convertview = = null) {Convertview = Inflater.inflate ( R.layout.assist_device_binding_list_item, null); holder = new Viewholder (); HOLDER.CB = (CheckBox) Convertview.findviewbyid (r.id.checkbox1); holder.tvname = (TextView) Convertview.findviewbyid (R.id.tv_device_name) ; Convertview.settag (holder);} else {//Remove Holderholder = (viewholder) Convertview.gettag ();} Holder.tvName.setText (bean);//monitor checkbox and set a new state holder.cb.setOnClickListener based on the original status (new View.onclicklistener () { public void OnClick (View v) {if (Isselected.get (position)) {Isselected.put (position, false); setisselected (isSelected) ;} else {isselected.put (position, true); Setisselected (isselected);}}); /Set the checkbox's Check state holder.cb.setChecked (getisselected (). get (position)), and return Convertview, according to IsSelected; public static Hashmap<integer, boolean> getisselected () {return isSelected;} public static void Setisselected (Hashmap<integer, boolean> isSelected) {listviewadapter.isselected = isSelected;}}

You need a friend, just copy the changes.

Combination of ListView and checkbox in Android----multiple selection and recording

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.