के साथ ListView के अजीब व्यवहार में मेरे पास सूची दृश्य के ऊपर एक चेकबॉक्स है जिसमें चेकबॉक्स आइटम हैं। इसलिए जब मैं चेकबॉक्स की जांच करता हूं तो मैं सूची के सभी आइटमों की जांच करने के लिए notifyDataSetChanged()
लागू करता हूं। तो उस समय मुझे getView विधि के लिए दो बार लॉग मिल रहा है, इसका मतलब है कि जब मैं notifyDataSetChanged()
पर कॉल कर रहा हूं तो GetView को दो बार बुलाया जा रहा है। तो, क्या कोई मुझे बता सकता है कि मुझे दो बार लॉग क्यों मिल रहा है? इसके अलावा getView() को दो बार क्यों कहा जाता है?चेकबॉक्स
मुख्य वर्ग - एडाप्टर कक्षा के लिए
checkAll = (CheckBox) findViewById(R.id.my_check_box);
checkAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton checkbox, boolean arg1) {
adapter.notifyDataSetChanged();
}
});
कोड -
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 आउटपुट जब मैं adapter.notifyDataSetChanged();
CheckAll checkboxs पर श्रोता बदलें चेक कर सकते हैं।
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
आप देख सकते हैं कि मैं लॉगकैट आउटपुट मुझे दो बार एक ही आउटपुट प्राप्त कर रहा हूं। तो क्या कोई यह बता सकता है कि यह क्यों हो रहा है?
यह समस्या मेरे साथ भी बढ़ती है ... +1 –
http: // stackoverflow।कॉम/प्रश्न/2618272/कस्टम-लिस्टव्यू-एडाप्टर-गेटव्यू-विधि-जा रहा-बहु-बार-और-इन-नो-सह इस – MKJParekh
@ ललित पॉपटानी को स्क्रॉल करें क्या आप स्क्रॉल करते हैं? – Venky