Tengo un CheckBox encima del ListView que tiene elementos de casilla de verificación en él. Entonces, cuando marque la casilla de verificación, aplicaré notifyDataSetChanged()
para verificar todos los elementos de la lista. Entonces en ese momento estoy obteniendo registros para el método getView dos veces, significa que se llama a getView dos veces cuando llamo al notifyDataSetChanged()
solo una vez. Entonces, ¿alguien puede decirme por qué estoy recibiendo registros dos veces? ¿Por qué también getView() se llama dos veces?Weird Behavior of ListView with CheckBox
Clase Principal -
CódigocheckAll = (CheckBox) findViewById(R.id.my_check_box);
checkAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton checkbox, boolean arg1) {
adapter.notifyDataSetChanged();
}
});
para la clase Adaptador -
public class myAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
private CheckBox checkAll;
public myAdapter(Activity context, List<Model> list, CheckBox checkAll) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
this.checkAll = checkAll;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.row, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Model element = (Model) viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
if(checkAll.isChecked() == true){
System.out.println(position+" checked th position");
}
else if(checkAll.isChecked() == false){
System.out.println(position+" not checked th position");
}
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
}
LogCat salida Cuando puedo adapter.notifyDataSetChanged();
en checkboxs checkAll inspeccionar el cambio de escucha.
11-30 20:31:33.751: INFO/System.out(1300): 0 checked th position
11-30 20:31:33.761: INFO/System.out(1300): 1 checked th position
11-30 20:31:33.770: INFO/System.out(1300): 2 checked th position
11-30 20:31:33.780: INFO/System.out(1300): 3 checked th position
11-30 20:31:33.810: INFO/System.out(1300): 4 checked th position
11-30 20:31:33.810: INFO/System.out(1300): 0 checked th position
11-30 20:31:33.831: INFO/System.out(1300): 1 checked th position
11-30 20:31:33.831: INFO/System.out(1300): 2 checked th position
11-30 20:31:33.851: INFO/System.out(1300): 3 checked th position
11-30 20:31:33.860: INFO/System.out(1300): 4 checked th position
Puede ver que la salida de Logcat obtengo el mismo resultado dos veces. Entonces, ¿alguien puede decir por qué está sucediendo esto?
este problema también plantea conmigo ... +1 –
http: // stackoverflow.com/questions/2618272/custom-listview-adapter-getview-method-being-called-multiple-times-and-in-no-co compruebe esto – MKJParekh
@Lalit Poptani ¿Se desplaza? – Venky