Android 在ListView中使用CheckBox,实现增加联系人的功能

考虑到项目需要,在CheckBox的监听中加入了增加删除联系人数据的操作,在bean类中加入判断状态的Boolean变量,来存储点击状态

BaseAdapter类
public class PopEmpSelectAdapter extends BaseAdapter{
    private LayoutInflater mInflater;
    private List<EmployInfo> datas;
    private Context mContext;
    public PopEmpSelectAdapter(Context context) {
        mContext = context;
        mInflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setData(List<EmployInfo> datas) {
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas == null ? 0 : datas.size();
    }
    private class  Holder{
        CheckBox mCb_emp;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.pop_empselectadapter, parent,
                    false);
            holder = new Holder();
            holder.mCb_emp = (CheckBox) convertView.findViewById(R.id.cb_emp);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        final EmployInfo data = datas.get(position);
        if (data == null)
            return null;
       holder.mCb_emp.setText(data.getEmpname());

        //final Holder finalHolder = holder;

        final String str = data.getEmpname();
        final String num = data.getEmpid();
        holder.mCb_emp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(((CheckBox)v).isChecked()){
                    SessionHelper.getInstance().getEmp_str().add(str);
                    SessionHelper.getInstance().getEmp_num().add(num);
                    data.setCheckStatus(true);
                }else{
                    SessionHelper.getInstance().getEmp_str().remove(str);
                    SessionHelper.getInstance().getEmp_num().remove(num);
                    data.setCheckStatus(false);
                }
            }
        });
        holder.mCb_emp.setChecked(data.getCheckStatus());
        return convertView;
    }
    @Override
    public EmployInfo getItem(int position) {
        // TODO Auto-generated method stub
        return datas.get(position);
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
}

<strong><span style="color:#ff0000;">EmployInfo类部分代码</span></strong>
<span style="font-family: Arial, Helvetica, sans-serif;">private boolean checkStatus=true;</span>

public boolean getCheckStatus() {
	return checkStatus;
}
public void setCheckStatus(boolean checkStatus) {
	this.checkStatus = checkStatus;
}

SessionHelper部分代码

//新建邮箱增加的收件人str
	private List<String> emp_str = new ArrayList<String>();
	public List<String> getEmp_str() {
		return emp_str;
	}
	public void setEmp_str(List<String> emp_str) {
		this.emp_str = emp_str;
	}

	//新建邮箱增加的收件人num
	private List<String> emp_num =  new ArrayList<String>();
	public List<String> getEmp_num() {
		return emp_num;
	}
	public void setEmp_num(List<String> emp_num) {
		this.emp_num = emp_num;
	}

效果图



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值