简体   繁体   中英

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

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

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

在此处输入图片说明

The way the ListView works is that it recycles the same views populated with different data. Hence, the need for the view holder pattern, which it looks like you are using.

So, in order for your list to work correctly, you need to save the checked state to your backing data model. It looks like you might already have a "selected" boolean in the model? If so, you just need to save the state to this boolean.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM