Android中ListView结合CheckBox判断选中项
16lz
2021-01-25
Android中ListView结合CheckBox判断选中项
本文主要实现在自定义的ListView布局中加入CheckBox控件,通过判断用户是否选中CheckBox来对ListView的选中项进行相应的操作。通过一个Demo来展示该功能,选中ListView中的某一项,然后点击Button按钮来显示选中了哪些项。
[1] 程序结构图如下:
其中Person.java是实体类,MainActivity.java是Activity组件类。listitem.xml是自定义的列表每项布局文件。
[2] listitem.xml布局文件源码如下:
[html] view plain copy- <?xmlversion="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布局文件源码如下: [html] view plain copy
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns: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实体类源码如下: [html] view plain copy
- packagecom.andyidea.bean;
- publicclassPerson{
- privateStringname;
- privateStringaddress;
- publicStringgetName(){
- returnname;
- }
- publicvoidsetName(Stringname){
- this.name=name;
- }
- publicStringgetAddress(){
- returnaddress;
- }
- publicvoidsetAddress(Stringaddress){
- this.address=address;
- }
- }
[5] MainActivity.java类源码如下: [html] view plain copy
- packagecom.andyidea.listview;
- importjava.util.ArrayList;
- importjava.util.HashMap;
- importjava.util.List;
- importcom.andyidea.bean.Person;
- importandroid.app.Activity;
- importandroid.app.AlertDialog;
- importandroid.content.Context;
- importandroid.os.Bundle;
- importandroid.util.Log;
- importandroid.view.LayoutInflater;
- importandroid.view.View;
- importandroid.view.ViewGroup;
- importandroid.widget.BaseAdapter;
- importandroid.widget.Button;
- importandroid.widget.CheckBox;
- importandroid.widget.ListView;
- importandroid.widget.TextView;
- publicclassMainActivityextendsActivity{
- Buttonshow;
- ListViewlv;
- List<Person>persons=newArrayList<Person>();
- ContextmContext;
- MyListAdapteradapter;
- List<Integer>listItemID=newArrayList<Integer>();
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mContext=getApplicationContext();
- show=(Button)findViewById(R.id.show);
- lv=(ListView)findViewById(R.id.lvperson);
- initPersonData();
- adapter=newMyListAdapter(persons);
- lv.setAdapter(adapter);
- show.setOnClickListener(newView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- listItemID.clear();
- for(inti=0;i<adapter.mChecked.size();i++){
- if(adapter.mChecked.get(i)){
- listItemID.add(i);
- }
- }
- if(listItemID.size()==0){
- AlertDialog.Builderbuilder1=newAlertDialog.Builder(MainActivity.this);
- builder1.setMessage("没有选中任何记录");
- builder1.show();
- }else{
- StringBuildersb=newStringBuilder();
- for(inti=0;i<listItemID.size();i++){
- sb.append("ItemID="+listItemID.get(i)+".");
- }
- AlertDialog.Builderbuilder2=newAlertDialog.Builder(MainActivity.this);
- builder2.setMessage(sb.toString());
- builder2.show();
- }
- }
- });
- }
- /**
- *模拟数据
- */
- privatevoidinitPersonData(){
- PersonmPerson;
- for(inti=1;i<=12;i++){
- mPerson=newPerson();
- mPerson.setName("Andy"+i);
- mPerson.setAddress("GuangZhou"+i);
- persons.add(mPerson);
- }
- }
- //自定义ListView适配器
- classMyListAdapterextendsBaseAdapter{
- List<Boolean>mChecked;
- List<Person>listPerson;
- HashMap<Integer,View>map=newHashMap<Integer,View>();
- publicMyListAdapter(List<Person>list){
- listPerson=newArrayList<Person>();
- listlistPerson=list;
- mChecked=newArrayList<Boolean>();
- for(inti=0;i<list.size();i++){
- mChecked.add(false);
- }
- }
- @Override
- publicintgetCount(){
- returnlistPerson.size();
- }
- @Override
- publicObjectgetItem(intposition){
- returnlistPerson.get(position);
- }
- @Override
- publiclonggetItemId(intposition){
- returnposition;
- }
- @Override
- publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
- Viewview;
- ViewHolderholder=null;
- if(map.get(position)==null){
- Log.e("MainActivity","position1="+position);
- LayoutInflatermInflater=(LayoutInflater)mContext
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- view=mInflater.inflate(R.layout.listitem,null);
- holder=newViewHolder();
- 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);
- finalintp=position;
- map.put(position,view);
- holder.selected.setOnClickListener(newView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- CheckBoxcb=(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());
- returnview;
- }
- }
- staticclassViewHolder{
- CheckBoxselected;
- TextViewname;
- TextViewaddress;
- }
- }
[6] 程序运行后的结果如下:
文章出处:http://blog.csdn.net/cjjky/article/details/6967219
更多相关文章
- 一款常用的 Squid 日志分析工具
- GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
- RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
- Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
- [原创] 在Android系统使用socket在framework和native之间通信
- Android使用Fragment实现底部导航栏
- Android* 教程:使用英特尔® 线程构建模块编写多线程应用
- Android远程Service(AIDL)的简单实例
- Android源代码加入SDK,在程序中查看android源代码