listview中checkbox错乱

package cn.timetask.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.StrikethroughSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import cn.timetask.R;
import cn.timetask.entity.Task;
import cn.timetask.utils.LogUtil;
import cn.timetask.utils.StringUtil;

/**
 * 四象限列表详细展示 第一象限
 * 最开始4个象限是一个adapter  
 * 后来分开是为了考虑因为convertView缓存导致的问题
 * 
 * @author linhui
 * 2013-11-7下午4:36:32
 */
public class TaskFirstInfoListAdapter extends BaseAdapter{

	private Context mContext;
	private List<Task> list;
	private Task task;
	//用来记录checkbox的状态   key为position  value为勾选状态
	private Map<Integer,Boolean> ckboxState = new HashMap<Integer, Boolean>();
	private Integer currentItemPos;
	
	public TaskFirstInfoListAdapter(Context mContext, List<Task> list) {
		this.mContext = mContext;
		this.list = list;
		if(list!=null){
			for (int i=0;i<list.size();i++) {
				Task t = list.get(i);
				if (StringUtil.notEmpty(t.getFlag())) {
					if (t.getFlag().equals("1")) {
						ckboxState.put(i, true);
					} else {
						ckboxState.put(i, false);
					}
				}
			}
		}
	}
	
	@Override
	public int getCount() {
		LogUtil.d("getCount"," getCount()");
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		LogUtil.d("getItem", "getItem()");
		return null;
	}

	@Override
	public long getItemId(int position) {
		LogUtil.d("getItem", "getItemId()");
		return list.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView,ViewGroup parent) {
		this.currentItemPos = position;
		LogUtil.d("getView", "getView() currentItemPos="+currentItemPos+"~~position="+position);
		 task = list.get(position);
		 ViewHolder  holder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext,R.layout.task_info_first_list, null);
            holder = new ViewHolder();
            holder.checkButton = (CheckBox) convertView.findViewById(R.id.task_info_list_button);
            holder.text =  (TextView) convertView.findViewById(R.id.task_info_list_taskcontent);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        LogUtil.d("setTag", "setTag currentItemPos="+currentItemPos+"~~position="+position);
        //设置tag
        holder.checkButton.setTag(position);
        //进行判断是否check
    	if(ckboxState.get(position)){
    		LogUtil.d("getView checked", position+"");
    		holder.checkButton.setChecked(true);
        }else{
    		holder.checkButton.setChecked(false);
			ckboxState.put(position,false);
       }
        holder.checkButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				CheckBox ck = (CheckBox)v;
				LogUtil.d("buttonView.getTag().toString()", v.getTag().toString());
				LogUtil.d("setOnClickListener isChecked", ck.isChecked()+"");
                                //保存当前的状态记录
				ckboxState.put(Integer.parseInt(v.getTag().toString()), ck.isChecked());
				notifyDataSetChanged();
			}
		});
      /* holder.checkButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				LogUtil.d("buttonView.getTag().toString()", buttonView.getTag().toString());
				LogUtil.d("onCheckedChanged isChecked", isChecked+"");
				ckboxState.put(Integer.parseInt(buttonView.getTag().toString()), isChecked);
			}
		});*/
        return convertView;
    }
    
    class ViewHolder {
        CheckBox checkButton;
        TextView  text;
    }
}

1.万恶的setOnCheckedChangeListener,我之前使用的这个来监听,后来打log发现,只要你滑动listview,滑上去了,这个自动调用了,然后checkbox自动被置为false了。

2.holder.checkButton.setTag(position);这个一定要记得放在 if (convertView == null) 判断的外面,不然滑动下去后,checkbox容易出现错乱


你可能感兴趣的:(listview中checkbox错乱)