删掉代码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人 整理发布,转载须标明出处。
原文地址:http://blog.csdn.net/androidmylove/article/details/42078679