答案中的代码有效,但效率很低(实际上,您可以看到它,只需滚动
ListView并检查
Logcat即可看到垃圾收集器正在工作)。下面是一种改进的
getView回收视图的方法:
@Overridepublic View getView(int position, View convertView, ViewGroup parent) { LinearLayout view = (LinearLayout) convertView; if (view == null) { view = (LinearLayout) inflater.inflate(R.layout.record_view_start, parent, false); } TextView tv = (TextView) view.findViewById(R.id.engName); tv.setText(getItem(position)); CheckBox cBox = (CheckBox) view.findViewById(R.id.checkBox1); cBox.setTag(Integer.valueOf(position)); // set the tag so we can identify the correct row in the listener cBox.setChecked(mChecked[position]); // set the status as we stored it cBox.setonCheckedChangeListener(mListener); // set the listener return view;}onCheckedChangeListener mListener = new onCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { mChecked[(Integer)buttonView.getTag()] = isChecked; // get the tag so we know the row and store the status }};关于您问题中的代码,起初我认为这是错误的,因为您设置行的方式不同,但是我不明白为什么当您从列表中分离行视图时适配器会有这种行为。另外,我什至测试了代码,它在
CheckBoxes(但内存处理非常差)方面表现很好。也许您正在做其他事情使适配器无法工作?



