I have a ListView
with CheckBox
, this ListView
is organized with two List in the adapter, one that would be all items, and the other only with those selected.
I created a CheckBox
to add the markup / deselect all functionality, so when I click mark, I add all the items in the selected list, otherwise I create a new instance of the list, / p>
However, if I click on a CheckBox
within ListView
I also add / remove the selected list, and I call notifySetDataChanged () to refresh the screen, the strange thing is that it is also being removed from the main list , which causes the screen to be redrawn without the item, not just uncheck it.
At any point in the code do I make any changes to the main list, only in the selected list, and even then, when I will add / remove from the selected list is being removed from the main list, does anyone know what may be occurring?
The adapter code is this:
public class ListViewNaoEnviadosAdapter extends BaseAdapter {
private List<NaoEnviado> naoEnviados;
private List<NaoEnviado> selecionados;
private CheckBox chkEnviarSmsNaoEnviados, chkNaoEnviadosMarcarTodos;
private TextView tvTotalNaoEnviados;
private LayoutInflater layoutInflater;
private final Context ctx;
public ListViewNaoEnviadosAdapter(Context ctx, List<NaoEnviado> naoEnviados) {
this.layoutInflater = LayoutInflater.from(ctx);
this.ctx = ctx;
this.naoEnviados = naoEnviados;
this.selecionados = naoEnviados;
chkNaoEnviadosMarcarTodos = (CheckBox) ((MainActivity) ctx)
.findViewById(R.id.chkNaoEnviadosMarcarTodos);
chkNaoEnviadosMarcarTodos.setChecked(true);
tvTotalNaoEnviados = (TextView) ((MainActivity) ctx)
.findViewById(R.id.tvTotalNaoEnviados);
tvTotalNaoEnviados.setText(String.valueOf(selecionados.size()));
}
@Override
public int getCount() {
return naoEnviados.size();
}
@Override
public Object getItem(int position) {
return naoEnviados.get(position);
}
@Override
public long getItemId(int position) {
return naoEnviados.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
NaoEnviadosHelper naoEnviadosHelper = new NaoEnviadosHelper();
final NaoEnviado naoEnviado = naoEnviados.get(position);
final Telefone tel = new Telefone(ctx);
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.listview_sms_nao_enviados, null);
naoEnviadosHelper.nome = (TextView) convertView.findViewById(R.id.tvSmsNaoEnviadosNome);
naoEnviadosHelper.telefone = (TextView) convertView.findViewById(R.id.tvSmsNaoEnviadosTelefone);
naoEnviadosHelper.descFalha = (TextView) convertView.findViewById(R.id.tvSmsNaoEnviadosDescFalha);
naoEnviadosHelper.enviarSms = (CheckBox) convertView.findViewById(R.id.chkEnviarSmsNaoEnviados);
convertView.setTag(naoEnviadosHelper);
} else {
naoEnviadosHelper = (NaoEnviadosHelper) convertView.getTag();
}
naoEnviadosHelper.nome.setText(naoEnviado.getNome().trim());
naoEnviadosHelper.telefone.setText(tel.formataTelefone(naoEnviado.getTelefone()));
naoEnviadosHelper.descFalha.setText(naoEnviado.getTipoFalha());
naoEnviadosHelper.enviarSms.setChecked(selecionados.contains(naoEnviado));
//Checkbox marcar/desmarcar todos na tela...
chkNaoEnviadosMarcarTodos = (CheckBox) ((MainActivity) ctx)
.findViewById(R.id.chkNaoEnviadosMarcarTodos);
chkNaoEnviadosMarcarTodos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selecionados = new ArrayList<NaoEnviado>();
if (chkNaoEnviadosMarcarTodos.isChecked()) {
selecionados = naoEnviados;
}
notifyDataSetChanged();
}
});
//Checkbox marcar/desmarcar para envio
chkEnviarSmsNaoEnviados = (CheckBox) convertView.findViewById(R.id.chkEnviarSmsNaoEnviados);
chkEnviarSmsNaoEnviados.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!chkEnviarSmsNaoEnviados.isChecked()) {
selecionados.add(naoEnviado);
} else {
//Aqui acontece o problema, quando mando remover desta lista, também
// é removido da lista principal (naoEnviados)
selecionados.remove(naoEnviado);
//Se um elemento foi removido eu desmarco o checkbox marcartodos
chkNaoEnviadosMarcarTodos.setChecked(false);
}
notifyDataSetChanged();
}
});
tvTotalNaoEnviados.setText(String.valueOf(selecionados.size()));
return convertView;
}
private class NaoEnviadosHelper {
TextView telefone, nome, descFalha;
CheckBox enviarSms;
}
}