ListView specific Operation method of Android checkbox _android

Source: Internet
Author: User
Tags static class

The main implementation of this article is to add a CheckBox control to a custom ListView layout, by determining whether the user selects a checkbox to do the appropriate action on the ListView selected item. Show the feature through a demo, select an item in ListView, and then click the button button to show which items are selected.
1. The program structure chart is as follows:

Where Person.java is the entity class, Mainactivity.java is the activity component class. Listitem.xml is a custom list for each layout file.
2, listitem.xml layout file source code 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:orientati on= "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=" 1 0DP "/> <textview android:id=" @+id/list.address android:layout_width= "Fill_parent" Android:layout_hei ght= "Wrap_content" android:layout_weight= "1" android:text= "Address" android:layout_gravity= "center" android:textsize= "20DP"/> </linear 
 Layout> </LinearLayout>

3, main.xml layout file source code 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, Person.java entity class source code 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 the activity is a. 
  * * @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 () {@Override public void OnClick (View v) {ListItem 
    Id.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.th 
     IS); 
     Builder1.setmessage ("No Records 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 ();
    } 
   } 
  }); 
  /** * Analog 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); 
  @Override public int GetCount () {return listperson.size (); 
  @Override public Object getitem (int position) {return listperson.get (position); 
  @Override public long getitemid (int position) {return position; } @Override PublIC view GetView (int position, view Convertview, ViewGroup parent) {view 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 () {@Override public 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; 
 } 
}

The results of the program run are as follows:

I hope this article will help you learn about Android software programming.

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.