CheckBox Select all, reverse, calculate price, delete

Source: Internet
Author: User

Activity_main.xml

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:orien tation= "vertical" > <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_conten T "> <button android:id=" @+id/button1 "android:layout_width=" Wrap_content "Android:lay out_height= "Wrap_content" android:text= "Select All" android:layout_weight= "1"/> <button and Roid:id= "@+id/button2" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Andro id:text= "Reverse Select" android:layout_weight= "1"/> <button android:id= "@+id/button3" Androi D:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "All not selected" Android:layout         _weight= "1"/> <buttonAndroid:id= "@+id/button4" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" an droid:text= "Delete" android:layout_weight= "1"/></linearlayout><textview android:id= "@+id/price "Android:layout_width=" wrap_content "android:layout_height=" wrap_content "/><listview android:id=" @+id        /listveiw "android:layout_width=" match_parent "android:layout_height=" Match_parent "></ListView> </LinearLayout>

 

Listveiw.xml

If the ListView item contains a checkbox, the ListView will not have a checked state when the ListView is selected because the focus priority of the checkbox is higher than the ListView. The workaround is to add a property to the CheckBox component: Android:focusable= "false"

 

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent ">   <textview        android:id= "@+id/text"       android:layout_width= "wrap_content"       android:layout_height= "Wrap_content"       android:text= "ddd"              />   <checkbox       android:id= "@+id/checkbox"       android:layout_width= " Wrap_content "       android:layout_height=" wrap_content "              android:focusable=" false "          android:clickable= "False"       android:layout_alignparentright= "true"       android:layout_alignparenttop= "true"/></ Relativelayout>

Mainactivity.java

Package Com.bawei.day04_listview;import Java.util.arraylist;import Java.util.iterator;import java.util.List;import Com.bawei.day04_listview. Adper.viewholder;import Com.bawei.vo.good;import Android.os.bundle;import Android.app.activity;import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.baseadapter;import Android.widget.button;import Android.widget.listview;import    Android.widget.textview;public class Mainactivity extends Activity {private Button button1;    Private Button button2;    Private Button Button3;    Private ListView ListView;    Private list<good> List;    Private TextView Price;    private int num=0;    private int pric=0;    Private Baseadapter adapter;    Private Button Button4;    Private list<good> Li; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Button1 = (Button) Findviewbyid (r.id.button1);//Select All Button2 = (Button) Findviewbyid (r.id.button2);//Counter-select Button 3 = (Button) Findviewbyid (r.id.button3);//Select All Button4 = (Button) Findviewbyid (r.id.button4);//Delete ListView =        (ListView) Findviewbyid (R.ID.LISTVEIW);//Price = (TextView) Findviewbyid (R.id.price);        List = new arraylist<good> ();        Assignment for (int i = 0; i < i++) {List.add (New Good (i+ "", false));        }//Adaptation adapter = new Adper (list,mainactivity.this);        Listview.setadapter (adapter);                Select All Button1.setonclicklistener (new Onclicklistener () {public void OnClick (View v) {                 TODO auto-generated method Stub num = 0;                pric = 0;               for (int i = 0; i < list.size (); i++) {//change Boolean     List.get (i). Setbo (True);                         If you select if (List.get (i). Getbo ()) {num++;                                        Pric+=integer.parseint (List.get (i). GetName ());                }}//Refresh adapter.notifydatasetchanged ();                            Display Price.settext ("+num+" is selected, "+" Price is "+pric+");        }        });                Inverse selection Button2.setonclicklistener (new Onclicklistener () {public void OnClick (View v) {                 TODO auto-generated method Stub num = 0;                pric = 0;                        for (int i = 0; i < list.size (); i++) {//Change value if (List.get (i). Getbo ()) {                    List.get (i). Setbo (false);                    }else{List.get (i). Setbo (True);      }//Refresh              Adapter.notifydatasetchanged ();                         If you select if (List.get (i). Getbo ()) {num++;                                            Pric+=integer.parseint (List.get (i). GetName ());            }}//Show Price.settext with TextView ("+num+", "+" Price is "+pric+" yuan);        }        });                All Button3.setonclicklistener (new Onclicklistener () {public void OnClick (View v) {                 TODO auto-generated method Stub num = 0;                pric = 0;                    for (int i = 0; i < list.size (); i++) {//Change value list.get (i). Setbo (false);                    Refresh adapter.notifydatasetchanged ();                         If you select if (List.get (i). Getbo ()) {num++; Pric+=integer.parseinT (List.get (i). GetName ());            }} price.settext ("+num+" is selected altogether, "+" Price is "+pric+");        }        });        Delete 1/*li = new arraylist<good> (); Button4.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {// TODO auto-generated method stub for (int i = 0; i < list.size (); i++) {if (List.get (i                                            ). Getbo ()) {//Save up Li.add (List.get (i)) to be deleted;                }}//delete List.removeall (LI);                 Adapter.notifydatasetchanged ();                 num = 0;                pric = 0;            Price.settext ("A total of selected" +num+ "pieces," + "Price is" +pric+ "Yuan"); }}); *//delete 2 Button4.setonclicklistener (new Onclicklistener () {public void ONclick (View v) {//TODO auto-generated Method Stub//Get Iterator for list collection Iterator                It=list.iterator ();                    while (It.hasnext ()) {//Gets the corresponding set element good g= (good) it.next ();                    Determines if (G.getbo ()) {//Removes the element returned by the last next method from the collection It.remove ();                 }}//Refresh adapter.notifydatasetchanged ();                 num = 0;                 pric = 0;            Display Price.settext ("+num+" is selected, "+" Price is "+pric+");        }        }); The listener that binds the ListView Listview.setonitemclicklistener (new Onitemclicklistener () {public void Onitemclick (A Dapterview<?> arg0, View arg1, int arg2, long arg3) {//TODO auto-generated Metho D stub//Get Viewholder object Viewholder Viewholder = (viewholder) Arg1.gettag ();                 Change the status of the CheckBox ViewHolder.checkBox.toggle ();                 Record the check status of the CheckBox List.get (arg2). Setbo (viewHolder.checkBox.isChecked ());                      Adjusts the selected entry if (viewHolder.checkBox.isChecked () = = true) {num++;                    Pric+=integer.parseint (List.get (arg2). GetName ());                      } else {num--;                    Pric-=integer.parseint (List.get (arg2). GetName ());            }//With TextView display Price.settext ("+num+"), "+" Price is "+pric+" yuan);            }        }); }    }

List of Vo class Good.java

Package Com.bawei.vo;public class Good {private String name;private boolean bo;public string GetName () {    return name;} public void SetName (String name) {    this.name = name;} public Boolean Getbo () {    return bo;} public void Setbo (Boolean bo) {    this.bo = bo;} @Overridepublic String toString () {    return "good [name=" + name + ", bo=" + Bo + "]";} Public good (String name, Boolean bo) {    super ();    this.name = name;    This.bo = Bo;} Public good () {    super ();}}

Fitting Adper.java

Package Com.bawei.day04_listview;import Java.util.list;import Com.bawei.vo.good;import android.content.Context; Import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.button;import Android.widget.checkbox;import Android.widget.compoundbutton;import Android.widget.compoundbutton.oncheckedchangelistener;import    Android.widget.textview;public class Adper extends Baseadapter {list<good> List;        Context context;    Public Adper (list<good> List, Context context) {//TODO auto-generated constructor stub this.list=list;            This.context=context;    } public int GetCount () {//TODO auto-generated Method stub return List.size ();    } public Object GetItem (int position) {//TODO auto-generated method stub return null;    } public long Getitemid (int position) {//TODO auto-generated method stub return 0;    }Public View GetView (final int position, View Convertview, ViewGroup parent) {//TODO auto-generated method stub        Final Viewholder Viewholder;         if (convertview==null) {convertview=view.inflate (context, r.layout.listveiw, null);         Viewholder=new Viewholder ();        viewholder.textview= (TextView) Convertview.findviewbyid (R.id.text);         viewholder.checkbox= (CheckBox) Convertview.findviewbyid (R.id.checkbox);        Convertview.settag (Viewholder);        }else{viewholder= (Viewholder) Convertview.gettag ();                } viewHolder.textView.setText ("Price:" +list.get (position). GetName ());                Displays the CheckBox viewHolder.checkBox.setChecked (list.get (position). GETBO ());            return convertview;    }class viewholder{TextView TextView; checkbox checkbox;}}

  

CheckBox Select all, reverse, calculate price, delete

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.