解决ListView中有多个Checkbox的时候点击的问题
来源:互联网 发布:黄金看盘软件 编辑:程序博客网 时间:2024/05/22 10:25
在ListView中放置checkbox的话,如果有一个checkbox,那么可以将checkbox的事件在ListView的item事件中进行处理,那么如果有多个checkbox就不方便了,今天有同学问我这个怎么做,之前我也没做过,就给了思路给他,结果同学还是没做出来,我就自己写了个Demo,将代码发出来,以便以后自己用,也希望能帮到有需要的朋友,代码比较简单,就不多赘述了,代码中也有注释;
布局文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/listview" ></ListView></RelativeLayout>
ListView的item文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <CheckBox android:id="@+id/cb1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项1" /> <CheckBox android:id="@+id/cb2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项2" /> <CheckBox android:id="@+id/cb3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项3" /> <CheckBox android:id="@+id/cb4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项4" /></LinearLayout>Java代码:
package com.example.test;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.ListView;import android.widget.CompoundButton.OnCheckedChangeListener;public class MainActivity extends Activity {private ListView listview;private List<String> list;private Map<Integer,Boolean> map;private Map<Integer,Map<Integer,Boolean>> map2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);list=new ArrayList<String>();for (int i = 0; i < 30; i++) {list.add(""+i+i+i+i);}map=new HashMap<Integer, Boolean>();map2=new HashMap<Integer, Map<Integer,Boolean>>();listview=(ListView) findViewById(R.id.listview);MyAdapter adapter=new MyAdapter();listview.setAdapter(adapter);}class MyAdapter extends BaseAdapter{@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int position) {return list.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {View view=null; ViewHolder holder=null;if(convertView!=null){view=convertView;holder=(ViewHolder) view.getTag();}else{holder=new ViewHolder();view=View.inflate(MainActivity.this, R.layout.item, null);holder.cb1=(CheckBox) view.findViewById(R.id.cb1);holder.cb2=(CheckBox) view.findViewById(R.id.cb2);holder.cb3=(CheckBox) view.findViewById(R.id.cb3);holder.cb4=(CheckBox) view.findViewById(R.id.cb4);view.setTag(holder);}//给每个checkbox设置这个checkbox属于的item的position,以便在点击事件中获取所属的item 的索引值holder.cb1.setTag(position);holder.cb2.setTag(position);holder.cb3.setTag(position);holder.cb4.setTag(position);//用于存放每个item中checkbox的状态final Map<Integer,Boolean> mapList=new HashMap<Integer, Boolean>();holder.cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {//获取checkbox所属的item 的positionint i=(Integer)buttonView.getTag();//将checkbox的状态放到一个map中mapList.put(1, isChecked);//将这个map作为值放到大的map中,用position作为键map2.put(i, mapList);}});holder.cb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {int i=(Integer)buttonView.getTag();mapList.put(2, isChecked);map2.put(i, mapList);}});holder.cb3.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {int i=(Integer)buttonView.getTag();mapList.put(3, isChecked);map2.put(i, mapList);}});holder.cb4.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {int i=(Integer)buttonView.getTag();mapList.put(4, isChecked);map2.put(i, mapList);}});Map<Integer,Boolean> map3=map2.get(position);if(map3!=null){if(map3.get(1)!=null){holder.cb1.setChecked(map3.get(1));}else{holder.cb1.setChecked(false);}if(map3.get(2)!=null){holder.cb2.setChecked(map3.get(2));}else{holder.cb2.setChecked(false);}if(map3.get(3)!=null){holder.cb3.setChecked(map3.get(3));}else{holder.cb3.setChecked(false);}if(map3.get(4)!=null){holder.cb4.setChecked(map3.get(4));}else{holder.cb4.setChecked(false);}}else{holder.cb1.setChecked(false);holder.cb2.setChecked(false);holder.cb3.setChecked(false);holder.cb4.setChecked(false);}return view;}}class ViewHolder{CheckBox cb1;CheckBox cb2;CheckBox cb3;CheckBox cb4;}}
0 0
- 解决ListView中有多个Checkbox的时候点击的问题
- ListView的Item包含checkBox的时候的点击事件
- 解决:ListView+CheckBox 点击错乱问题
- ListView点击没有反应,(其每一项有button或checkbox子控件的时候),解决。。
- ListView 数据错乱问题的解决(CheckBox)
- 解决ListView含有CheckBox时ListView不响应的问题
- ListView和ListItem中的CheckBox点击冲突的问题
- ListView中有Checkbox时的点击和选择问题
- ListView的item中有button ImageButton CheckBox EditText等时 点击事件失效问题的解决
- ListView的item中有button ImageButton CheckBox EditText等时 点击事件失效问题的解决
- 做高德导航时候一个关于listview点击的问题
- ListView 和CheckBox的批量删除问题的解决。
- ListView点击checkbox其他checkbox也被同时选中的问题
- ListView点击checkbox其他checkbox也被同时选中的问题
- ListView点击checkbox其他checkbox也被同时选中的问题
- ListView点击checkbox其他checkbox也被同时选中的问题
- ListView+CheckBox解决复选框混乱的问题
- 解决 listview 中添加 checkbox 出现的问题
- 写于母亲节
- Linux init系统
- 参加挑战杯
- 第四章 字符串和数组
- NSDate, NSDateFormatter的使用
- 解决ListView中有多个Checkbox的时候点击的问题
- 关于ssh整合jbpm4.4 无法获取processEngine的问题
- 分治与动态规划
- 考试系统维护--不同版本SQL数据导入导出
- Qt 2048
- POJ 题目1995 Raising Modulo Numbers(快速幂)
- 大仙说道之Android studio实现Service AIDL
- (DS 《算法竞赛入门经典》)LA 3644 X-Plosives(并查集)
- candy