Adapter 为 ListView 对 android 滚动问题

发布时间: 2016/10/10 20:07:24
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我已经创建了 adapter 为我 listview .为每个有的 listitem checkbox 上它。每当我滚动我 listview 它随机设置 checkbox 检查项目。

下面是我的 getView 方法

public View getView(int position, View convertView, final ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.list_equipment_search, null);

    TextView serialno = (TextView) vi.findViewById(R.id.txtSerialNo); // Serial No
    TextView unitno = (TextView) vi.findViewById(R.id.txtUnitNo); // Unit No
    CustomCheckBox chkEquipment = (CustomCheckBox) vi.findViewById(R.id.chkEquipment); // Select CheckBox


    final Equipment equipment = data.get(position);

    // Setting all values in listview
    serialno.setText(equipment.GetSerialNo());
    unitno.setText(equipment.GetUnitNo());
    if (Equipment.EquipmentInList(equipment, selected))
        chkEquipment.setChecked(true, false);


    chkEquipment.setOnCheckedChangeListener((EquipmentSearchActivity) context);


    return vi;
}

守则 》 Checkbox 改变的是

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    int pos = ((ListView) findViewById(R.id.listEquipment)).getPositionForView(buttonView);

    if (pos != ListView.INVALID_POSITION) {
        Equipment e = equipmentList.get(pos);
        if (isChecked && !Equipment.EquipmentInList(e, SelectedEquipment))
            SelectedEquipment.add(e);
        else if (!isChecked)
            SelectedEquipment = Equipment.RemoveEquipmentFromList(e, SelectedEquipment);
    }

    Button btnStoreEquipment = (Button) findViewById(R.id.btnStoreEquipment);

    if (SelectedEquipment.size() > 0)
        btnStoreEquipment.setVisibility(View.VISIBLE);
    else
        btnStoreEquipment.setVisibility(View.INVISIBLE);
}

解决方法 1:

试试这个。

public View getView(int position, View convertView, final ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.list_equipment_search, null);
    enter code here
    TextView serialno = (TextView) vi.findViewById(R.id.txtSerialNo); // Serial No
    TextView unitno = (TextView) vi.findViewById(R.id.txtUnitNo);

 CheckBox checkbox = (CheckBox) row.findViewById(R.id.chkEquipment);
        checkbox.setTag(new Integer(position));
        checkbox.setOnCheckedChangeListener(null);

            if (checkBoxState[position])
                checkbox.setChecked(true);
            else
                checkbox.setChecked(false);



        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Integer pos = (Integer) buttonView.getTag();
                if (isChecked) {

                    checkBoxState[pos.intValue()] = true;
                } else {
                    checkBoxState[pos.intValue()] = false;


                }
            }
        });
赞助商