码迷,mamicode.com
首页 > 移动开发 > 详细

android listview中自定义适配器和复选框

时间:2014-12-22 14:33:28      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:android   android开发   

下面的代码中报错了,代码如下

@Override
p
public View getView(final int position, final View convertView, final ViewGroup parent) {
  
    final ViewHolder viewHolder;
  
    View view = convertView;
  
    if (view == null) {
  
        view = LayoutInflater.from(mContext).inflate(R.layout.row, parent, false);
  
        viewHolder = new ViewHolder();
 
 
 
        viewHolder.textTitle = (TextView) view.findViewById(R.id.title);
  
        viewHolder.checkBox = (CheckBox) view.findViewById(R.id.checkBox);
 
 
 
        viewHolder.checkBox.setTag(position);
  
        view.setTag(viewHolder);
 
 
 
        viewHolder.imageView = (ImageView) view.findViewById(R.id.activity_googlecards_card_imageview);
  
    } else {
  
        viewHolder = (ViewHolder) view.getTag();
  
        viewHolder.checkBox.getTag(position);
  
    }
 
 
 
    viewHolder.textTitle.setText(getItem(position).getTitle());
 
 
 
    viewHolder.checkBox.setChecked(myIntegerArrayList.contains(position));
  
    viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  
        @Override
  
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  
            if (isChecked) {
  
                myIntegerArrayList.add(position);
  
            } else {
  
                myIntegerArrayList.remove((Object) position);
  
            }
  
        }
  
    });
 
 
 
    return view;
}
}
 
 
 
 
private static class ViewHolder {
  
    TextView textTitle;
  
    CheckBox checkBox;
}
}


该复选框本身工作正常。位置正确添加到我的ArrayList,但如果(假设我选中的项目在位置1)我把listview滚动到底部,然后再滚回去,选中取消了....

 

处理方法

删掉代码viewHolder.checkBox.setTag(position); 
把 viewHolder.checkBox.getTag();放到  onCheckedChanged 中,你按照下面的代码改动

@Override

public View getView(final int position, final View convertView, final ViewGroup parent) {
  
    final ViewHolder viewHolder;
  
    View view = convertView;
  
    if (view == null) {
  
        view = LayoutInflater.from(mContext).inflate(R.layout.row, parent, false);
  
        viewHolder = new ViewHolder();
 
 
 
        viewHolder.textTitle = (TextView) view.findViewById(R.id.title);
  
        viewHolder.checkBox = (CheckBox) view.findViewById(R.id.checkBox);
 
 
 
 
        view.setTag(viewHolder);
 
 
 
        viewHolder.imageView = (ImageView) view.findViewById(R.id.activity_googlecards_card_imageview);
  
    } else {
  
        viewHolder = (ViewHolder) view.getTag();
  
        viewHolder.checkBox.getTag(position);
  
    }
  
    viewHolder.checkBox.setTag(position);
 
 
 
    viewHolder.textTitle.setText(getItem(position).getTitle());
 
 
 
    viewHolder.checkBox.setChecked(myIntegerArrayList.contains(position));
  
    viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  
        @Override
  
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  
            if (isChecked) {
  
                myIntegerArrayList.add(viewHolder.checkBox.getTag());
  
            } else {
  
                myIntegerArrayList.remove((Object) viewHolder.checkBox.getTag());
  
            }
  
        }
  
    });
 
 
 
    return view;
}
}


让你删掉 viewHolder.checkBox.setTag(position); 是为了 回收ListView,你需要报错位置在tag中,并让 onCheckedChanged 获得到

添加viewHolder.checkBox.getTag() 也是同样的原因


原文地址:http://www.itmmd.com/201411/119.html 
该文章由 萌萌的IT人 整理发布,转载须标明出处。

android listview中自定义适配器和复选框

标签:android   android开发   

原文地址:http://blog.csdn.net/androidmylove/article/details/42078679

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!