CheckBox and ListView

Source: Internet
Author: User

package com.example.selectall;import java.util.arraylist;import java.util.list;import  android.app.activity;import android.os.bundle;import android.view.view;import  android.view.view.onclicklistener;import android.widget.arrayadapter;import android.widget.checkbox; import android.widget.listview;import android.widget.spinner;import  android.widget.spinneradapter;import android.widget.textview;public class mainactivity  extends activity {    private listview listview;     private TextView tvCount;    private CheckBox checkAll;     private MyAdapter adapter;    private List<User>  datas;    private boolean isselectall;//Select All (Learn to play this)      private int selectcount;//statistics selected number of     @oVerride    protected void oncreate (bundle savedinstancestate)  {         super.oncreate (savedinstancestate);         setcontentview (R.layout.activity_main);         Initview ();         initdata ();         adapter = new myadapter (datas, this);         listview.setadapter (Adapter);         Checkall.setonclicklistener (New onclicklistener ()  {              @Override              Public void onclick (VIEW&NBSP;V)  {                 isselectall = ! isselectall;                // Update views with the Select All button                  Updateselectallstatus ();            }         });     }    private void  updateselectallstatus ()  {        for (User user:datas) {             user.setchecked (ISSELECTALL);         }        if  ( Isselectall)  {            selectcount =  datas.size ();        }else{             selectCount = 0;        }         tvcount.settext ("You have selected" +selectcount+ ");         adapter.notifydatasetchanged ();    }    private void  InitData ()  {        datas = new arraylist<user > ();        user user;         for (int i= 0;i<15;i++) {             user = new user ("name" +i,10+i);             datas.add (user);        }             }    private void initview ()  {        listview =  (ListView)  findviewbyid (R.id.listview);         tvCount =  (TextView)  findviewbyid (r.id.tvcount);         checkAll =  (CheckBox)  findviewbyid (r.id.checkall);     }    public void oprate (string oprate) {         if  ("Add". Equals (oprate))  {             selectcount++;        }else if ("Sub ". Equals (oprate)) {            selectcount--;         }                 if  (Selectcount==datas.size ())  {           &nBsp; checkall.setchecked (True);             isselectall = true;        }else{             checkall.setchecked (False);             isSelectAll = false;         }        tvcount.settext ("You have selected" +selectCount+ ");     }}
package com.example.selectall;import java.util.list;import android.content.context;import  Android.view.layoutinflater;import android.view.view;import android.view.view.onclicklistener;import  android.view.ViewGroup;import android.webkit.WebView.FindListener;import  Android.widget.baseadapter;import android.widget.checkbox;import android.widget.textview;public  class MyAdapter extends BaseAdapter {    private List< User> datas;    private context context;    public  myadapter ( list<user> datas,context context)  {         this.context = context;        this.datas  = datas;    }     @Override     public  int getcount () &NBSP;{&NBSP;&NBSP;&NBsp;     return datas.size ();     }    @ Override    public object getitem (int position)  {         return datas.get (position);    }      @Override     public long getitemid (int position)  {         return position;    }    @ Override    public view getview (Int position, view convertview,  viewgroup parent)  {        ViewHolder viewHolder;         if  (convertview==null)  {             convertview = layoutinflater.from (context). Inflate ( R.layout.item, null);              viewholder = new viewholder ( );            viewholder.tvname =  ( TextView)  convertview.findviewbyid (r.id.name);             viewHolder.tvage =  (TextView)  convertview.findviewbyid (r.id.age);             viewHolder.checkBox =  (CheckBox)   Convertview.findviewbyid (R.id.check);             / /set a tag   to fetch objects from the cache              Convertview.settag (Viewholder);        }else{             viewHolder =  (Viewholder)  convertview.gettag ();         }        final user user =  Datas.get (position);         viewholder.tvname.settext (User.getName ());         viewholder.tvage.settext (User.getage () + "");         viewholder.checkbox.setchecked (user.ischecked ());         viewholder.checkbox.setonclicklistener (New onclicklistener ()  {              @Override              public void onclick (VIEW&NBSP;V)  {                 boolean ischecked = ! User.ischecked ();                 user.setchecked (isChecked);                 if  (isChecked )  {                     //(Learn to play this)                       ((mainactivity) context). Oprate ("add");                 }else{                     //minus                       ( mainactivity). Oprate ("sub");                 }            }          });         return convertview;    }     class ViewHolder{        TextView  tvname,tvage;        checkbox checkbox;    }}
package com.example.selectall;public class user {    private  string name;    private int  age;    private  Boolean ischecked;    public user ()  {    }     public user (string name, int age)  {         this.name = name;        this.age =  Age;    }    public string getname ()  {         return name;    }    public  void setname (String name)  {        this.name  = name;    }    public int getage ()  {       &nBsp; return age;    }    public void setage (int  age)  {        this.age = age;     }    public boolean ischecked ()  {         return isChecked;    }    public void  Setchecked (boolean ischecked)  {        this.ischecked =  ischecked;    }    }


CheckBox and ListView

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.