3

I am using custom adpater in AlertDialog. In that adapter, i am using TextView and CheckBox.. Now I want to handle CheckBox's setOnCheckedChangeListener for checking checked or unchecked CheckBox.. And according to status of CheckBox, I want to implement some code. But this listener is fired more than one time.. So how can I handle it? Suggest me if anyone has an idea.

And exactly my problem is when i checked on checkbox, i want to increment some value and when i unchecked, i want to decrement some value.but i am not getting exact sum value and if i scroll then this sum value is changed.. so what i have to do?

Following is my Custom Adaper :

private class Updateinfo_ServiceAdapter extends BaseAdapter
{

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return _options_services.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Viewholder holder;
        LayoutInflater inflater=getLayoutInflater();

        if(convertView==null)
        {
            convertView=inflater.inflate(R.layout.row_updateinfo_service, null);
            holder=new Viewholder();
            holder.txtname=(TextView)convertView.findViewById(R.id.serviceName);
            holder.chkSelected=(CheckBox)convertView.findViewById(R.id.chk);
            convertView.setTag(holder);
        }
        else
        {
            holder=(Viewholder)convertView.getTag();
        }

        holder.txtname.setText(_options_services[position]);        
        holder.chkSelected.setChecked(_selections_services[position]);



        holder.chkSelected.setOnCheckedChangeListener(new OnCheckedChangeListener() 
        {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
            {

                if (isChecked) {
                    allowServicesSum = allowServicesSum
                            + Integer.parseInt((String) services[position]
                                    .getSystemServiceID());
                    System.out.println("AllowService sum is "+allowServicesSum);
                } else {
                    allowServicesSum = allowServicesSum
                            - Integer.parseInt((String) services[position]
                                    .getSystemServiceID());
                    System.out.println("AllowService sum is "+allowServicesSum);
                }
            }
        });

               if(_selections_services[position])
        {
            holder.chkSelected.setEnabled(false);
        }
        else
        {
            holder.chkSelected.setEnabled(true);
        }

        return convertView;
    }

    private class Viewholder
    {
        TextView txtname;
        CheckBox chkSelected;
    }

}
Kinjal Shah
  • 536
  • 4
  • 13

1 Answers1

3

You've forgotten that in the case where you reuse the existing convertView, the listener is already set. Thus, when you do holder.chkSelected.setChecked, that will trigger the listener that was set the previous time you returned this view from getView.

The easiest way to avoid the problem is to call holder.chkSelected.setOnCheckedChangeListener(null); immediately after the line holder=(Viewholder)convertView.getTag();. This makes sure there is no listener when you later call setChecked, after which you can add the listener again.

Dan Hulme
  • 14,779
  • 3
  • 46
  • 95
  • i called listener immediately after gettag.. but not works.. my problem is when i checked on checkbox, i want to increment some value and when i unchecked, i want to decrement some value. but if i scroll then this sum value is changed.. so what i have to do? – Kinjal Shah Aug 06 '13 at 11:27
  • I updated my answer to make it clear that you have to **unset** the listener. Sorry that wasn't clear initially. – Dan Hulme Aug 06 '13 at 11:37
  • if i unset the listener then still there is problem..if i checked the item and scroll down then that selected item will be unchecked automatically. so what should be done here ? – Kinjal Shah Aug 06 '13 at 11:59
  • That's because you're setting the checkbox's state from `_selections_services[position]`, but you don't update that array when the checkbox is checked or unchecked. – Dan Hulme Aug 06 '13 at 12:05
  • Ya you are right.. now i put that _selections_services[position] true or false according to checkbox staus.so now it works fine..Thanks a lot.but now concern is that by default i checked some items and i want this items disabled.. so how can i achieve ? please see my updated question for disabling checkbox.. – Kinjal Shah Aug 06 '13 at 12:46
  • I've answered your question now. You can't just keep changing it to add more things until we've written your whole app for you. – Dan Hulme Aug 06 '13 at 13:27