其他分享
首页 > 其他分享> > 带有自定义适配器包含复选框的Alertdialg及其侦听器

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

作者:互联网

我在AlertDialog中使用自定义adpater.在那个适配器中,我正在使用TextView和CheckBox.
现在,我想处理CheckBox的setOnCheckedChangeListener以便检查已选中或未选中的CheckBox.并且根据CheckBox的状态,我想实现一些代码.但是此监听器被解雇了不止一次..那么我该如何处理呢?如果有人有主意,建议我.

正是我的问题是当我在复选框上选中时,我想增加一些值,而当我未选中时,我想减少一些值.但是我没有得到确切的总和值,如果我滚动然后更改了总和值..我该怎么办?

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

}

解决方法:

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

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

标签:android-checkbox,android-alertdialog,custom-adapter,android
来源: https://codeday.me/bug/20191122/2063713.html