6

I am having a CheckBox above the ListView having Checkbox items in it. So when I check the Checkbox I apply notifyDataSetChanged() to check all the check all the items of the List. So at that time I am getting logs for getView method two times, it means getView is getting called twice when I am calling notifyDataSetChanged() only once. So, can anyone tell me why I am getting logs twice? Also why getView() is called twice?

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

Code for Adapter 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 When I can adapter.notifyDataSetChanged(); on CheckAll checkboxs Check Change 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

You can see that I the Logcat output I am getting the same output two times. So can anyone tell the why this is happening?

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • 2
    this problem also raise with me ... +1 – Nikunj Patel Dec 01 '11 at 11:16
  • 2
    http://stackoverflow.com/questions/2618272/custom-listview-adapter-getview-method-being-called-multiple-times-and-in-no-co check this – MKJParekh Dec 01 '11 at 11:35
  • @Lalit Poptani Do you scroll? – Venky Dec 01 '11 at 11:38
  • @Venky no I am not scrolling I only have 4 items. – Lalit Poptani Dec 01 '11 at 11:40
  • 1
    @LalitPoptani Hello I am scrolling and multiple select and select all , single select . Single , multiple , select all working fine but when scrolling then position is changes, and all deselect. So how can I solve that issue. – Md Maidul Islam Jan 20 '14 at 14:42
  • @Venky Hello I am scrolling and multiple select and select all , single select . Single , multiple , select all working fine but when scrolling then position is changes, and all deselect. So how can I solve that issue. – Md Maidul Islam Jan 20 '14 at 14:43

3 Answers3

5

Try making your ListView height fill_parent.

Lisiview try itself to adjust a bit as per space provided, this is what I learned, and looks like this is the reason behind.

See if it helps.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
viv
  • 6,158
  • 6
  • 39
  • 54
3

This is not really an answer to your question - it's just that it hurts my eyes a little when I see this:

if(checkAll.isChecked() == true){
    System.out.println(position+" checked th position");
}
else if(checkAll.isChecked() == false){
    System.out.println(position+" not checked th position");
}

The if-statement requires a boolean expression and since the method isChecked() returns a boolean theres no need to match it up against true/false. Also since a boolean can only be true or false there's no need to check for both. In short you can boil the above down to:

if( checkAll.isChecked() )
  System.out.println(position+" checked th position");
else
  System.out.println(position+" not checked th position");

Which is much prettier :-)

Do not see this as critique of your programming skills - just see it as a learning opportunity.

Katana
  • 401
  • 3
  • 22
kaspermoerch
  • 16,127
  • 4
  • 44
  • 67
  • Well its just an Demo App for getting the exact idea about my problem. Thanks than too. – Lalit Poptani Dec 01 '11 at 11:43
  • That's hilarious, kaspermoerch! Try this: `System.out.println(position + (checkAll.isChecked() ? " checked th position" : " not checked th position"));` – Jeff Lawson Dec 03 '14 at 16:45
  • That works too, Jeff. I do find though, that the ternary operator should be used with caution. It doesn't always improve readability. – kaspermoerch Dec 04 '14 at 14:00
0

this is bacause viewHolder inside checkchangelistener will change along with list scrolling . so no mapping betwen i'th checkbox and viewHolder can acheve using this way .

create sepatera class checkchangelistener implements checnChecnglistener . pass checkchangelistener through constructor .

EG. outside getView

class MyCheckChangeLsitenet implements CompoundButton.OnCheckedChangeListener

ViewHolder viewHolder
 MyCheckChangeLsitenet(ViewHolder viewHolder)
 {
this.viewHolder =  viewHolder ;
}
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            Model element = (Model) viewHolder.checkbox.getTag();
                            element.setSelected(buttonView.isChecked());
                        }
                    });

in GetView

viewHolder.checkbox .setOnCheckedChangeListener(new MycheckChangeListenet(viewHolder))

Shailendra Singh Rajawat
  • 8,172
  • 3
  • 35
  • 40