---
title: "listview中添加checkbox,并分别添加两者的点击事件"
author: "ableouou"
date: "2011-05-16T10:40:37Z"
tags: []
categories: ["移动开发"]
slug: "listview-checkbox"
draft: false
lastmod: "2011-05-16T10:40:37Z"
id: 1045275
url: "https://ableouou.iteye.com/blog/1045275"
---

要想在listview中添加checkbox，我的做法如下：
    首先，写一个抽象类BaseAdapter的继承类MyAdapter，里面主要实现的方法有：
View getView(int position, View convertView, ViewGroup parent) {


			ViewHolder holder = null;//ViewHolder是自己
//定义的一个结构体

			if (convertView == null) {

				holder = new ViewHolder();

				convertView = mInflater.inflate(R.layout.blacklist_import_item, null);

				holder.cbBlack = (CheckBox) convertView.findViewById(R.id.cb_choose_contact);

				holder.name = (TextView) convertView.findViewById(R.id.tv_blacklist_importer_name);

				holder.phone = (TextView) convertView.findViewById(R.id.tv_blacklist_importer_phone);

				

				convertView.setTag(holder);

			} else {

				holder = (ViewHolder) convertView.getTag();

			}



			holder.name.setText((String) telphones.get(position).get("linker_name"));

			holder.phone.setText((String) telphones.get(position).get("linker_phone"));
			final String number=(String) telphones.get(position).get("linker_phone");
			holder.cbBlack.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
										//点击操作
				}
			

			});

			return convertView;

		
}

//结构体viewHolder
public final class ViewHolder {

		public CheckBox cbBlack;

		public TextView name;

		public TextView phone;

		

	}