Android adds checkbox implementation to listview

Source: Internet
Author: User

The main interface CheckBoxinListViewActivity. the java code is as follows: [java] public class CheckBoxinListViewActivity extends Activity {/** Called when the activity is first created. */private MyAdapter adapter; private ListView; private Button checkAll; private Button noCheckAll; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); listview = (L IstView) findViewById (R. id. listview); checkAll = (Button) findViewById (R. id. button1); noCheckAll = (Button) findViewById (R. id. button2); adapter = new MyAdapter (); listview. setAdapter (adapter); checkAll. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {adapter. checkAll () ;}}); noCheckAll. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {Adapter. noCheckAll () ;}});} private class MyAdapter extends BaseAdapter {private ArrayList <Message> list = new ArrayList <Message> (); public MyAdapter () {for (int I = 1; I <= 100; I ++) {list. add (new Message ("item _" + I) ;}} public void checkAll () {for (Message msg: list) {msg. isCheck = true;} policydatasetchanged ();} public void noCheckAll () {for (Message msg: list) {msg. isCheck = false;} policydat ASetChanged () ;}@ Override public int getCount () {return list. size () ;}@ Override public Object getItem (int position) {return null ;}@ Override public long getItemId (int position) {return 0 ;} @ Override public View getView (int position, View convertView, ViewGroup parent) {ViewHolder viewHolder; if (convertView = null) {LayoutInflater inflater = LayoutInflater. from (CheckBoxinListViewActivity. t His); convertView = inflater. inflate (R. layout. listview_item, null); viewHolder = new ViewHolder (); viewHolder. checkBox = (CheckBox) convertView. findViewById (R. id. checkBox1); convertView. setTag (viewHolder);} else {viewHolder = (ViewHolder) convertView. getTag ();} final Message msg = list. get (position); viewHolder. checkBox. setText (msg. str); viewHolder. checkBox. setChecked (msg. isCheck); // note that onChe is not set here CkedChangListener is worth thinking about viewHolder. checkBox. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {if (msg. isCheck) {msg. isCheck = false;} else {msg. isCheck = true ;}}); return convertView ;}} private class ViewHolder {CheckBox checkBox ;}} the Message adapted by the adapter. java: [java] public class Message {public boolean isCheck; public String str; public Message (String str) {This. str = str ;}} the main. xml Code is as follows: [html] <? 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 = "wrap_content" android: layout_height = "wrap_content"> <Button android: text = "select all" android: id = "@ + id/button1" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </Button> <Button android: text = "undo" android: id = "@ + id/button2" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </Button> </LinearLayout> <ListView android: id = "@ + id/listview" android: layout_height = "fill_parent" android: layout_width = "fill_parent"/> </LinearLayout> the listview_item.xml code is as follows: [html] <? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: descendantFocusability = "blocksDescendants"> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: gravity = "center"> <CheckBox android: text = "CheckBox" android: id = "@ + id/checkBox1" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> </LinearLayout>

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.