ListView _Checkbox
package com.h3c.ListTest;
import java.util.ArrayList;import java.util.List;
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.CompoundButton;import android.widget.TextView;import android.widget.CompoundButton.OnCheckedChangeListener;
public class ListTestAdapter extends BaseAdapter { Context context; List<String> list; ArrayList<String> selectList = new ArrayList<String>(); // 是否全选 private boolean isSelectAll = false;
public ListTestAdapter(ListTest listTest, List<String> list) { // TODO Auto-generated constructor stub context = listTest; this.list = list; }
@Override public int getCount() { // TODO Auto-generated method stub return list.size(); }
@Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); }
@Override public long getItemId(int position) { // TODO Auto-generated method stub return position; }
@Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder; if (convertView == null) { final LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.check_layout, null); holder = new ViewHolder(); holder.mTextView = (TextView) convertView .findViewById(R.id.TextView01_checklayout); holder.mCheckBox = (CheckBox) convertView .findViewById(R.id.CheckBox01_checklayout); convertView.setTag(holder); // 在初始化的时候判断是否被全选 if (this.isSelectAll == true) { holder.mCheckBox.setChecked(true); } else { holder.mCheckBox.setChecked(false); } } else { holder = (ViewHolder) convertView.getTag(); } holder.mTextView.setText(list.get(position));
//监听函数 holder.mCheckBox .setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton button, boolean isSelected) { ArrayList<String> list = ListTestAdapter.this.selectList;
if (isSelected == true) { if (list.size() > 0) { String people = ListTestAdapter.this.list.get(position); if (list.indexOf(people) < 0) { list.add(people); } } else { list.add(ListTestAdapter.this.list.get(position)); } } else { if (list.size() > 0) { String people = ListTestAdapter.this.list .get(position); for (int i = 0; i < list.size(); i++) { if (people == list.get(i)) { list.remove(i); } } } } } }); //为了防止错位 Object b = (Object) getItem(position); if(b != null){ holder.mCheckBox.setChecked(selectList.contains(b)); } return convertView; }
/** * 得到已选择的条数 * * @return */ public int getCheckedSize() { return this.selectList.size(); }
/** * 获得选择的数据列表 * * @return */ public ArrayList<String> getChecked() { return this.selectList; }
/** * 是否全选 * * @return */ public boolean isSelectAll() { return isSelectAll; }
/** * 设置全选 * * @param isSelectAll */ public void setSelectAll(boolean isSelectAll) { this.isSelectAll = isSelectAll; if (isSelectAll) { addAllChecked(); } else { removeAllChecked(); } }
/** * 选中所有的选项 */ public void addAllChecked() { removeAllChecked(); for (int i = 0; i < list.size(); i++) { selectList.add(list.get(i)); } }
/** * 清除所有的选项 */ public void removeAllChecked() { selectList.clear(); }
class ViewHolder { TextView mTextView; CheckBox mCheckBox; }}
import java.util.ArrayList;import java.util.List;
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.CompoundButton;import android.widget.TextView;import android.widget.CompoundButton.OnCheckedChangeListener;
public class ListTestAdapter extends BaseAdapter { Context context; List<String> list; ArrayList<String> selectList = new ArrayList<String>(); // 是否全选 private boolean isSelectAll = false;
public ListTestAdapter(ListTest listTest, List<String> list) { // TODO Auto-generated constructor stub context = listTest; this.list = list; }
@Override public int getCount() { // TODO Auto-generated method stub return list.size(); }
@Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); }
@Override public long getItemId(int position) { // TODO Auto-generated method stub return position; }
@Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder; if (convertView == null) { final LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.check_layout, null); holder = new ViewHolder(); holder.mTextView = (TextView) convertView .findViewById(R.id.TextView01_checklayout); holder.mCheckBox = (CheckBox) convertView .findViewById(R.id.CheckBox01_checklayout); convertView.setTag(holder); // 在初始化的时候判断是否被全选 if (this.isSelectAll == true) { holder.mCheckBox.setChecked(true); } else { holder.mCheckBox.setChecked(false); } } else { holder = (ViewHolder) convertView.getTag(); } holder.mTextView.setText(list.get(position));
//监听函数 holder.mCheckBox .setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton button, boolean isSelected) { ArrayList<String> list = ListTestAdapter.this.selectList;
if (isSelected == true) { if (list.size() > 0) { String people = ListTestAdapter.this.list.get(position); if (list.indexOf(people) < 0) { list.add(people); } } else { list.add(ListTestAdapter.this.list.get(position)); } } else { if (list.size() > 0) { String people = ListTestAdapter.this.list .get(position); for (int i = 0; i < list.size(); i++) { if (people == list.get(i)) { list.remove(i); } } } } } }); //为了防止错位 Object b = (Object) getItem(position); if(b != null){ holder.mCheckBox.setChecked(selectList.contains(b)); } return convertView; }
/** * 得到已选择的条数 * * @return */ public int getCheckedSize() { return this.selectList.size(); }
/** * 获得选择的数据列表 * * @return */ public ArrayList<String> getChecked() { return this.selectList; }
/** * 是否全选 * * @return */ public boolean isSelectAll() { return isSelectAll; }
/** * 设置全选 * * @param isSelectAll */ public void setSelectAll(boolean isSelectAll) { this.isSelectAll = isSelectAll; if (isSelectAll) { addAllChecked(); } else { removeAllChecked(); } }
/** * 选中所有的选项 */ public void addAllChecked() { removeAllChecked(); for (int i = 0; i < list.size(); i++) { selectList.add(list.get(i)); } }
/** * 清除所有的选项 */ public void removeAllChecked() { selectList.clear(); }
class ViewHolder { TextView mTextView; CheckBox mCheckBox; }}

浙公网安备 33010602011771号