Tôi đang có một CheckBox phía trên ListView có các mục Checkbox trong đó. Vì vậy, khi tôi kiểm tra hộp kiểm tôi áp dụng notifyDataSetChanged()
để kiểm tra tất cả các kiểm tra tất cả các mục của danh sách. Vì vậy, tại thời điểm đó tôi nhận được các bản ghi cho phương thức getView hai lần, nó có nghĩa là getView được gọi hai lần khi tôi gọi notifyDataSetChanged()
chỉ một lần. Vì vậy, bất cứ ai có thể cho tôi biết lý do tại sao tôi nhận được các bản ghi hai lần? Ngoài ra tại sao getView() được gọi hai lần?Hành vi lạ của ListView với CheckBox
Main Class -
checkAll = (CheckBox) findViewById(R.id.my_check_box);
checkAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton checkbox, boolean arg1) {
adapter.notifyDataSetChanged();
}
});
Mã cho Adaptor Class -
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 Output Khi tôi adapter.notifyDataSetChanged();
trên checkboxs CheckAll có thể Kiểm tra Thay đổi Listener.
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
Bạn có thể thấy rằng tôi đầu ra Logcat Tôi nhận được cùng một đầu ra hai lần. Vì vậy, bất cứ ai có thể cho biết lý do tại sao điều này đang xảy ra?
vấn đề này cũng tăng lên với tôi ... +1 –
http: // stackoverflow.com/questions/2618272/custom-listview-adapter-getview-phương pháp-được gọi là-nhiều lần-và-in-no-co kiểm tra này – MKJParekh
@Lalit Poptani Bạn có cuộn không? – Venky