0

我有一个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;
            }

解决了!

4

1 回答 1

0

Android reuses a previous row when scrolling; some of them being already checked. You'll need to set it directly:

                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))
于 2013-03-25T14:51:50.410 回答