我有ArrayAdapter
个自定义组件和复选框。当我检查一个项目并向下滚动时(在getView()
中创建了新项目,之前未经检查的CheckBoxes
被标记为已选中。我部分理解为什么会这样,但我该如何避免呢?
这是我的getView(...)
方法:
@Override
public View getView(final int position, final View convertView,
final ViewGroup parent) {
View v = convertView;
TagHolder tagHolder = null;
if (v == null) {
Log.d("Inside adapter getView()", "Extras have "
+ CurrentRequest.getCarDetalized().getExtra()
.size() + " items");
final LayoutInflater vi = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.additional_component, null);
tagHolder = new TagHolder();
tagHolder.extraName = (TextView) v
.findViewById(R.id.additional_info);
tagHolder.extraPrice = (TextView) v
.findViewById(R.id.additional_price);
v.setTag(tagHolder);
tagHolder.box = (CheckBox) v.findViewById(R.id.select);
tagHolder.box.setOnCheckedChangeListener(checkedItem);
} else {
tagHolder = (TagHolder) v.getTag();
}
try {
final CarDetails.Extra extra = getItem(position);
tagHolder.box.setTag(position);
tagHolder.extraName.setText(extra.getName());
tagHolder.extraPrice.setText(extra.getCurrency() + " "
+ extra.getPrice());
Log.d("Adapter",
"Set up adapter field with name " + extra.getName());
Log.d("Inside adapter", "extras size is "
+ d.getExtra().size());
} catch (final Exception e) {
e.printStackTrace();
System.exit(0);
Log.e("CarList", "Error setting cell" + e);
}
return v;
}
解决!
答案 0 :(得分:0)
Android在滚动时会重复使用上一行;其中一些已被检查。你需要直接设置它:
final CarDetails.Extra extra = getItem(position);
tagHolder.box.setTag(position);
tagHolder.extraName.setText(extra.getName());
tagHolder.extraPrice.setText(extra.getCurrency() + " "
+ extra.getPrice())
// Add this:
tagHolder.box.setChecked(myMethodToCheckIfItemIsChecked(extra))