为什么当我选择CheckBox滚动ListView时,它会被取消选择?

时间:2015-06-05 20:11:59

标签: android listview checkbox android-listview

我在这里看到一些帖子谈论这个,但我没有看到任何答案完成我的问题。

我有一些带有一些TextView和一个CheckBox的ListView,一切都很好,除了当我滚动列表时,一些选中的选项变为未选中状态。

这是我的getView适配器代码:

public View getView(int position, View convertView, ViewGroup parent) {
    Agendamento item = getItem(position);

    ViewHolder viewHolder;
    if (convertView == null) {
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.item_lista_agendamento, parent, false);

        viewHolder.campoNomeCliente = (TextView) convertView.findViewById(R.id.campoNomeCliente);
        viewHolder.campoNomePaciente = (TextView) convertView.findViewById(R.id.nomePaciente);
        viewHolder.campoNomeMedico = (TextView) convertView.findViewById(R.id.campoNomeMedico);
        viewHolder.campoCorStatus = (TextView) convertView.findViewById(R.id.campoCorStatus);
        viewHolder.campoCorStatus = (TextView) convertView.findViewById(R.id.campoCorStatus);
        viewHolder.campoDataHoraInc = (TextView) convertView.findViewById(R.id.campoDataHoraInc);

        CheckBox chk = (CheckBox) convertView.findViewById(R.id.marcado);

        viewHolder.chkMarcado = chk;
        viewHolder.chkMarcado.setTag(position);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    String codigoMedico = item.getCodigoMedico();
    String codigoCliente = item.getCodigoCliente();

    Medico medico = rep.getMedicoPorCodigo(codigoMedico);
    Cliente cliente = rep.getClientePorCodigo(codigoCliente);

    String status = item.getStatus().trim();
    int backgroundColor = 0;
    if (status.equals("S")) {
        backgroundColor = R.color.corSeparada;
    }
    if (status.equals("F")) {
        backgroundColor = R.color.corFinalizada;
    }
    if (status.equals("E")) {
        backgroundColor = R.color.corEncerrada;
    }
    if (status.equals("N")) {
        backgroundColor = R.color.corNova;
    }

    String dataInc = item.getDataInclusao().replaceAll("[/]", "");
    String dataBr = "";
    if (!dataInc.trim().equals("")) {
        String dia = dataInc.substring(4, 6);
        String mes = dataInc.substring(6, 8);
        String ano = dataInc.substring(0, 4);
        dataBr = dia + "/" + mes + "/" + ano;
    }
    String horaInc = item.getHoraInclusao().trim();
    dataBr += " " + horaInc;

    viewHolder.campoCorStatus.setBackgroundResource(backgroundColor);
    viewHolder.campoNomeCliente.setText(cliente == null ? "(cliente)" : cliente.getNome());
    viewHolder.campoNomePaciente.setText(item.getNomePaciente());
    viewHolder.campoNomeMedico.setText(medico == null ? "(médico)" : medico.getNome());
    viewHolder.campoDataHoraInc.setText(dataBr + " (" + item.getId() + ")");

    final Agendamento item1 = item;
    viewHolder.chkMarcado.setOnCheckedChangeListener(null);
    viewHolder.chkMarcado.setChecked(item.isSelected());
    viewHolder.chkMarcado.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                selectedList.add(item1);
            } else {
                selectedList.remove(item1);
            }
        }
    });

    if (position % 2 == 0) {
        convertView.setBackgroundResource(R.drawable.cor_zebra2);
    } else {
        convertView.setBackgroundResource(R.drawable.cor_zebra1);
    }

    // Return the completed view to render on screen
    return convertView;
}

以下是向上滚动之前的结果,向上滚动后,这些选中的复选框变为未选中状态:

enter image description here

1 个答案:

答案 0 :(得分:1)

ListView的工作方式是回收使用不同数据填充的相同视图。因此,需要视图持有者模式,它看起来像你正在使用。

因此,为了使列表正常工作,您需要将已检查状态保存到后备数据模型中。看起来你可能已经在模型中有一个“选定的”布尔值?如果是这样,您只需要将状态保存到此布尔值。

viewHolder.chkMarcado.setChecked(item.isSelected());
viewHolder.chkMarcado.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        item.setSelected(isChecked);
    }
});