包含复选框及其侦听器的自定义适配器的Alertdialog

时间:2013-08-06 10:02:42

标签: android android-alertdialog custom-adapter android-checkbox

我正在使用custom adpater in AlertDialog。在该适配器中,我正在使用TextView and CheckBox .. 现在我想处理CheckBox的setOnCheckedChangeListener来检查已检查或未检查的CheckBox。根据CheckBox的状态,我想实现一些代码。但是这个听众不止一次被解雇..所以我该如何处理呢?建议我,如果有人有想法。

确切地说我的问题是当我选中复选框时,我想增加一些值,当我取消选中时,我想减少一些值。但我没有得到精确的和值,如果我滚动那么这个总和值会改变..所以我必须做什么?

以下是我的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;
    }

}

1 个答案:

答案 0 :(得分:3)

您已经忘记了在重用现有convertView的情况下,已经设置了侦听器。因此,当您执行holder.chkSelected.setChecked时,将触发上一次从getView返回此视图时设置的侦听器。

避免此问题的最简单方法是在holder.chkSelected.setOnCheckedChangeListener(null);行后立即致电holder=(Viewholder)convertView.getTag();。这可以确保在您稍后调用setChecked时没有侦听器,之后您可以再次添加侦听器。