Android checkbox的listView具体操作方法
程序员文章站
2023-12-18 23:00:22
本文主要实现在自定义的listview布局中加入checkbox控件,通过判断用户是否选中checkbox来对listview的选中项进行相应的操作。通过一个demo来展示...
本文主要实现在自定义的listview布局中加入checkbox控件,通过判断用户是否选中checkbox来对listview的选中项进行相应的操作。通过一个demo来展示该功能,选中listview中的某一项,然后点击button按钮来显示选中了哪些项。
1、程序结构图如下:
其中person.java是实体类,mainactivity.java是activity组件类。listitem.xml是自定义的列表每项布局文件。
2、listitem.xml布局文件源码如下:
<?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="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:descendantfocusability="blocksdescendants"> <checkbox android:id="@+id/list.select" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <textview android:id="@+id/list.name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="name" android:layout_gravity="center" android:textsize="20dp" android:layout_marginleft="10dp"/> <textview android:id="@+id/list.address" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="address" android:layout_gravity="center" android:textsize="20dp"/> </linearlayout> </linearlayout>
3、 main.xml布局文件源码如下:
<?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"> <button android:id="@+id/show" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="show"/> <listview android:id="@+id/lvperson" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </linearlayout>
4、person.java实体类源码如下:
package com.andyidea.bean; public class person { private string name; private string address; public string getname() { return name; } public void setname(string name) { this.name = name; } public string getaddress() { return address; } public void setaddress(string address) { this.address = address; } }
5、mainactivity.java类源码如下:
package com.andyidea.listview; import java.util.arraylist; import java.util.hashmap; import java.util.list; import com.andyidea.bean.person; import android.app.activity; import android.app.alertdialog; import android.content.context; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.button; import android.widget.checkbox; import android.widget.listview; import android.widget.textview; public class mainactivity extends activity { button show; listview lv; list<person> persons = new arraylist<person>(); context mcontext; mylistadapter adapter; list<integer> listitemid = new arraylist<integer>(); /** called when the activity is first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); mcontext = getapplicationcontext(); show = (button)findviewbyid(r.id.show); lv = (listview)findviewbyid(r.id.lvperson); initpersondata(); adapter = new mylistadapter(persons); lv.setadapter(adapter); show.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { listitemid.clear(); for(int i=0;i<adapter.mchecked.size();i++){ if(adapter.mchecked.get(i)){ listitemid.add(i); } } if(listitemid.size()==0){ alertdialog.builder builder1 = new alertdialog.builder(mainactivity.this); builder1.setmessage("没有选中任何记录"); builder1.show(); }else{ stringbuilder sb = new stringbuilder(); for(int i=0;i<listitemid.size();i++){ sb.append("itemid="+listitemid.get(i)+" . "); } alertdialog.builder builder2 = new alertdialog.builder(mainactivity.this); builder2.setmessage(sb.tostring()); builder2.show(); } } }); } /** * 模拟数据 */ private void initpersondata(){ person mperson; for(int i=1;i<=12;i++){ mperson = new person(); mperson.setname("andy"+i); mperson.setaddress("guangzhou"+i); persons.add(mperson); } } //自定义listview适配器 class mylistadapter extends baseadapter{ list<boolean> mchecked; list<person> listperson; hashmap<integer,view> map = new hashmap<integer,view>(); public mylistadapter(list<person> list){ listperson = new arraylist<person>(); listperson = list; mchecked = new arraylist<boolean>(); for(int i=0;i<list.size();i++){ mchecked.add(false); } } @override public int getcount() { return listperson.size(); } @override public object getitem(int position) { return listperson.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { view view; viewholder holder = null; if (map.get(position) == null) { log.e("mainactivity","position1 = "+position); layoutinflater minflater = (layoutinflater) mcontext .getsystemservice(context.layout_inflater_service); view = minflater.inflate(r.layout.listitem, null); holder = new viewholder(); holder.selected = (checkbox)view.findviewbyid(r.id.list_select); holder.name = (textview)view.findviewbyid(r.id.list_name); holder.address = (textview)view.findviewbyid(r.id.list_address); final int p = position; map.put(position, view); holder.selected.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { checkbox cb = (checkbox)v; mchecked.set(p, cb.ischecked()); } }); view.settag(holder); }else{ log.e("mainactivity","position2 = "+position); view = map.get(position); holder = (viewholder)view.gettag(); } holder.selected.setchecked(mchecked.get(position)); holder.name.settext(listperson.get(position).getname()); holder.address.settext(listperson.get(position).getaddress()); return view; } } static class viewholder{ checkbox selected; textview name; textview address; } }
程序运行后的结果如下:
希望本文所述对大家学习android软件编程有所帮助。