vendredi 5 juin 2015

Why When I scroll my ListView with CheckBox selected it becomes unselected?

I have seen some posts here talking about this but I did not see any answer accomplishing to my problem.

I have this ListView with some TextViews and one CheckBox, everything is OK except that when I scroll the list some checked become unchecked.

Here is my getView adapter code:

    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);

        final Agendamento item1 = item;
        viewHolder.chkMarcado = chk;
        viewHolder.chkMarcado.setTag(position);
        viewHolder.chkMarcado.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    selectedList.add(item1);
                } else {
                    selectedList.remove(item1);
                }
            }
        });
        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() + ")");
    viewHolder.chkMarcado.setChecked(item.isSelected());

    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;
}

Here is the result before scrolling up, after scrolling up those checked checkboxes become unchecked:

enter image description here




Aucun commentaire:

Enregistrer un commentaire