Android Anti-Select, select All, delete

Source: Internet
Author: User

Mainactivity.class

@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        Findcomp ();        InitData (); adapter =NewMyadapter ( This, list); Lv.setadapter (adapter);//Select allAll.setonclicklistener (NewOnclicklistener () {@Override             Public void OnClick(View v) {//TODO auto-generated method stub                 for(intI=0; I<list.size (); i++) {List.get (i). Setisstate (true);            } adapter.notifydatasetchanged (); }        });//Delete, select the button listDel.setonclicklistener (NewOnclicklistener () {@Override             Public void OnClick(View v) {//TODO auto-generated method stub                //Iterate through the collection, or delete it if it is selected                 for(intI=0; I<list.size (); i++) {if(List.get (i). Isisstate ()) {//delete data from the collectionList.remove (i);/* * The data in the collection is moved up one bit in the list, so the position in the collection, * and the entry IDs in the list no longer match, so subtract 1 */i=i-1;            }} adapter.notifydatasetchanged (); }        });//Reverse selectionFan.setonclicklistener (NewOnclicklistener () {@Override             Public void OnClick(View v) {//TODO auto-generated method stub                 for(intI=0; I<list.size (); i++) {//Get the button for the check box in the collection                        BooleanIsstate=list.get (i). Isisstate ();//Put the status of the check box in the set, take the non, change the current stateList.get (i). Setisstate (!isstate); }//Refresh, ListAdapter.notifydatasetchanged ();    }        }); }//Add collection    Private void InitData() {list =NewArraylist<info> (); List.add (NewInfo ("Li bin")); List.add (NewInfo ("Li bin first machine")); List.add (NewInfo ("Li bin three standby")); List.add (NewInfo ("Li bin four-generation machine")); }

Myadapter.class

 Public  class myadapter extends baseadapter {     PublicContext context; PublicArraylist<info> list;PrivateViewhodler VH; Public Myadapter(Context context, arraylist<info> list) {Super(); This. Context = Context; This. list = list; }@Override     Public int GetCount() {//TODO auto-generated method stub        returnList.size (); }@Override     PublicObjectGetItem(intARG0) {//TODO auto-generated method stub        return NULL; }@Override     Public Long Getitemid(intARG0) {//TODO auto-generated method stub        return 0; }@Override     PublicViewGetView(Final intarg0, View Contrain, ViewGroup arg2) {//TODO auto-generated method stub        //Optimization        if(contrain==NULL) {contrain=view.inflate (context, R.layout.item,NULL); VH =NewViewhodler ();            Vh.title= (TextView) Contrain.findviewbyid (r.id.t_title);            vh.ck= (CheckBox) Contrain.findviewbyid (R.ID.C_CK);        Contrain.settag (VH); }Else{vh= (Viewhodler) Contrain.gettag ();        } vh.title.setText (List.get (arg0). GetTitle ()); Vh.ck.setChecked (List.get (arg0). Isisstate ());when the entry changes state, the changed state is stored in the set .Vh.ck.setOnCheckedChangeListener (NewOncheckedchangelistener () {@Override             Public void oncheckedchanged(Compoundbutton Buttonview,BooleanisChecked) {//TODO auto-generated method stubList.get (arg0). Setisstate (isChecked); }               });returnContrain; } Public  class viewhodler{        PrivateTextView title;PrivateCheckBox ck; }}

Android Anti-Select, select All, 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.