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

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

@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人 整理发布,转载须标明出处。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。