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