当前位置: 编程技术>移动开发
Android checkbox的listView(多选,全选,反选)具体实现方法
来源: 互联网 发布时间:2014-10-18
本文导语: 布局文件:[html] 代码如下: listView 的item布局文件:[html] 代码如下: Activity:[java] 代码如下:public clas...
布局文件:
[html]
listView 的item布局文件:
[html]
Activity:
[java]
public class Ex_checkboxActivity extends Activity {
private ListView lv;
private MyAdapter mAdapter;
private ArrayList list;
private Button bt_selectall;
private Button bt_cancel;
private Button bt_deselectall;
private int checkNum; // 记录选中的条目数量
private TextView tv_show;// 用于显示选中的条目数量
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* 实例化各个控件 */
lv = (ListView) findViewById(R.id.lv);
bt_selectall = (Button) findViewById(R.id.bt_selectall);
bt_cancel = (Button) findViewById(R.id.bt_cancelselectall);
bt_deselectall = (Button) findViewById(R.id.bt_deselectall);
tv_show = (TextView) findViewById(R.id.tv);
list = new ArrayList();
// 为Adapter准备数据
initDate();
// 实例化自定义的MyAdapter
mAdapter = new MyAdapter(list, this);
// 绑定Adapter
lv.setAdapter(mAdapter);
// 全选按钮的回调接口
bt_selectall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 遍历list的长度,将MyAdapter中的map值全部设为true
for (int i = 0; i < list.size(); i++) {
MyAdapter.getIsSelected().put(i, true);
}
// 数量设为list的长度
checkNum = list.size();
// 刷新listview和TextView的显示
dataChanged();
}
});
// 反选按钮的回调接口
bt_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 遍历list的长度,将已选的设为未选,未选的设为已选
for (int i = 0; i < list.size(); i++) {
if (MyAdapter.getIsSelected().get(i)) {
MyAdapter.getIsSelected().put(i, false);
checkNum--;
} else {
MyAdapter.getIsSelected().put(i, true);
checkNum++;
}
}
// 刷新listview和TextView的显示
dataChanged();
}
});
// 取消按钮的回调接口
bt_deselectall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 遍历list的长度,将已选的按钮设为未选
for (int i = 0; i < list.size(); i++) {
if (MyAdapter.getIsSelected().get(i)) {
MyAdapter.getIsSelected().put(i, false);
checkNum--;// 数量减1
}
}
// 刷新listview和TextView的显示
dataChanged();
}
});
// 绑定listView的监听器
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
// 取得ViewHolder对象,这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤
ViewHolder holder = (ViewHolder) arg1.getTag();
// 改变CheckBox的状态
holder.cb.toggle();
// 将CheckBox的选中状况记录下来
MyAdapter.getIsSelected().put(arg2, holder.cb.isChecked());
// 调整选定条目
if (holder.cb.isChecked() == true) {
checkNum++;
} else {
checkNum--;
}
// 用TextView显示
tv_show.setText("已选中" + checkNum + "项");
}
});
}
// 初始化数据
private void initDate() {
for (int i = 0; i < 15; i++) {
list.add("data" + " " + i);
}
}
// 刷新listview和TextView的显示
private void dataChanged() {
// 通知listView刷新
mAdapter.notifyDataSetChanged();
// TextView显示最新的选中数目
tv_show.setText("已选中" + checkNum + "项");
};
}
列表适配器:
[java]
package com.notice.listcheck;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
// 填充数据的list
private ArrayList list;
// 用来控制CheckBox的选中状况
private static HashMap isSelected;
// 上下文
private Context context;
// 用来导入布局
private LayoutInflater inflater = null;
// 构造器
public MyAdapter(ArrayList list, Context context) {
this.context = context;
this.list = list;
inflater = LayoutInflater.from(context);
isSelected = new HashMap();
// 初始化数据
initDate();
}
// 初始化isSelected的数据
private void initDate() {
for (int i = 0; i < list.size(); i++) {
getIsSelected().put(i, false);
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
// 获得ViewHolder对象
holder = new ViewHolder();
// 导入布局并赋值给convertview
convertView = inflater.inflate(R.layout.listviewitem, null);
holder.tv = (TextView) convertView.findViewById(R.id.item_tv);
holder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);
// 为view设置标签
convertView.setTag(holder);
} else {
// 取出holder
holder = (ViewHolder) convertView.getTag();
}
// 设置list中TextView的显示
holder.tv.setText(list.get(position));
// 根据isSelected来设置checkbox的选中状况
holder.cb.setChecked(getIsSelected().get(position));
return convertView;
}
public static HashMap getIsSelected() {
return isSelected;
}
public static void setIsSelected(HashMap isSelected) {
MyAdapter.isSelected = isSelected;
}
public static class ViewHolder {
TextView tv;
CheckBox cb;
}
}
[html]
代码如下:
listView 的item布局文件:
[html]
代码如下:
Activity:
[java]
代码如下:
public class Ex_checkboxActivity extends Activity {
private ListView lv;
private MyAdapter mAdapter;
private ArrayList list;
private Button bt_selectall;
private Button bt_cancel;
private Button bt_deselectall;
private int checkNum; // 记录选中的条目数量
private TextView tv_show;// 用于显示选中的条目数量
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* 实例化各个控件 */
lv = (ListView) findViewById(R.id.lv);
bt_selectall = (Button) findViewById(R.id.bt_selectall);
bt_cancel = (Button) findViewById(R.id.bt_cancelselectall);
bt_deselectall = (Button) findViewById(R.id.bt_deselectall);
tv_show = (TextView) findViewById(R.id.tv);
list = new ArrayList();
// 为Adapter准备数据
initDate();
// 实例化自定义的MyAdapter
mAdapter = new MyAdapter(list, this);
// 绑定Adapter
lv.setAdapter(mAdapter);
// 全选按钮的回调接口
bt_selectall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 遍历list的长度,将MyAdapter中的map值全部设为true
for (int i = 0; i < list.size(); i++) {
MyAdapter.getIsSelected().put(i, true);
}
// 数量设为list的长度
checkNum = list.size();
// 刷新listview和TextView的显示
dataChanged();
}
});
// 反选按钮的回调接口
bt_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 遍历list的长度,将已选的设为未选,未选的设为已选
for (int i = 0; i < list.size(); i++) {
if (MyAdapter.getIsSelected().get(i)) {
MyAdapter.getIsSelected().put(i, false);
checkNum--;
} else {
MyAdapter.getIsSelected().put(i, true);
checkNum++;
}
}
// 刷新listview和TextView的显示
dataChanged();
}
});
// 取消按钮的回调接口
bt_deselectall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 遍历list的长度,将已选的按钮设为未选
for (int i = 0; i < list.size(); i++) {
if (MyAdapter.getIsSelected().get(i)) {
MyAdapter.getIsSelected().put(i, false);
checkNum--;// 数量减1
}
}
// 刷新listview和TextView的显示
dataChanged();
}
});
// 绑定listView的监听器
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
// 取得ViewHolder对象,这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤
ViewHolder holder = (ViewHolder) arg1.getTag();
// 改变CheckBox的状态
holder.cb.toggle();
// 将CheckBox的选中状况记录下来
MyAdapter.getIsSelected().put(arg2, holder.cb.isChecked());
// 调整选定条目
if (holder.cb.isChecked() == true) {
checkNum++;
} else {
checkNum--;
}
// 用TextView显示
tv_show.setText("已选中" + checkNum + "项");
}
});
}
// 初始化数据
private void initDate() {
for (int i = 0; i < 15; i++) {
list.add("data" + " " + i);
}
}
// 刷新listview和TextView的显示
private void dataChanged() {
// 通知listView刷新
mAdapter.notifyDataSetChanged();
// TextView显示最新的选中数目
tv_show.setText("已选中" + checkNum + "项");
};
}
列表适配器:
[java]
代码如下:
package com.notice.listcheck;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
// 填充数据的list
private ArrayList list;
// 用来控制CheckBox的选中状况
private static HashMap isSelected;
// 上下文
private Context context;
// 用来导入布局
private LayoutInflater inflater = null;
// 构造器
public MyAdapter(ArrayList list, Context context) {
this.context = context;
this.list = list;
inflater = LayoutInflater.from(context);
isSelected = new HashMap();
// 初始化数据
initDate();
}
// 初始化isSelected的数据
private void initDate() {
for (int i = 0; i < list.size(); i++) {
getIsSelected().put(i, false);
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
// 获得ViewHolder对象
holder = new ViewHolder();
// 导入布局并赋值给convertview
convertView = inflater.inflate(R.layout.listviewitem, null);
holder.tv = (TextView) convertView.findViewById(R.id.item_tv);
holder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);
// 为view设置标签
convertView.setTag(holder);
} else {
// 取出holder
holder = (ViewHolder) convertView.getTag();
}
// 设置list中TextView的显示
holder.tv.setText(list.get(position));
// 根据isSelected来设置checkbox的选中状况
holder.cb.setChecked(getIsSelected().get(position));
return convertView;
}
public static HashMap getIsSelected() {
return isSelected;
}
public static void setIsSelected(HashMap isSelected) {
MyAdapter.isSelected = isSelected;
}
public static class ViewHolder {
TextView tv;
CheckBox cb;
}
}