复选框onclicklistener不在列表行android中工作

时间:2012-10-07 18:12:11

标签: android listview checkbox listactivity onclicklistener

我的列表行包含一个复选框以及其他一些视图。当我选中或取消选中该复选框时,不会触发其关联的动作侦听器。以下是getView()的代码片段。请看一下并向我提供解决方案。

    public View getView(int position, View convertView, ViewGroup parent) {
        //ViewHolder holder;

        if (convertView == null) 
        {
            convertView = mInflater.inflate(R.layout.list_item, null);
            Log.i("adapter", "ADAPTER");
        }

        checkTax = (CheckBox) convertView.findViewById(R.id.check_tax);
        checkTax.setOnCheckedChangeListener(new OnCheckedChangeListener () {      
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)          {
                        Log.i("on click", "ON CLICK");
                if (isChecked) {
                    tvSelectTaxAccount.setVisibility(View.VISIBLE);
                    spinnerTaxAcc.setVisibility(View.VISIBLE);
                    tvTaxAmt.setVisibility(View.VISIBLE);
                    editTaxAmt.setVisibility(View.VISIBLE);
                    tvAmtAfterTax.setVisibility(View.VISIBLE);
                    amtAfterTax.setVisibility(View.VISIBLE);
                    tvAmt.setText("amount before tax");
                    Log.i("checked", "CHECKED");
                }
                else
                {
                    tvSelectTaxAccount.setVisibility(View.GONE);
                    spinnerTaxAcc.setVisibility(View.GONE);
                    tvTaxAmt.setVisibility(View.GONE);
                    editTaxAmt.setVisibility(View.GONE);
                    tvAmtAfterTax.setVisibility(View.GONE);
                    amtAfterTax.setVisibility(View.GONE);
                    tvAmt.setText("AMOUNT");
                    Log.i("unchecked", "UNCHECKED");
                }
            }

        });

        tvSelectTaxAccount = (TextView) convertView.findViewById(R.id.tv_select_tax_account);
        spinnerTaxAcc = (Spinner) convertView.findViewById(R.id.spinner_tax_acc);
        spinnerTaxAcc.setOnItemSelectedListener(selectSpinnerTaxAcc);

        tvTaxAmt = (TextView) convertView.findViewById(R.id.tv_tax_amt);
        editTaxAmt = (EditText) convertView.findViewById(R.id.edit_tax_amt);
        tvAmt = (TextView) convertView.findViewById(R.id.tv_amt);
        tvAmtAfterTax = (TextView) convertView.findViewById(R.id.tv_amt_after_tax);
        amtAfterTax = (TextView) convertView.findViewById(R.id.amt_after_tax);

    spinnerTaxAcc.setOnItemSelectedListener(selectSpinnerTaxAcc);

        tvSelectTaxAccount.setVisibility(View.GONE);
        spinnerTaxAcc.setVisibility(View.GONE);

        tvTaxAmt.setVisibility(View.GONE);
        editTaxAmt.setVisibility(View.GONE);
        tvAmtAfterTax.setVisibility(View.GONE);
        amtAfterTax.setVisibility(View.GONE);

        return convertView;
    }

1 个答案:

答案 0 :(得分:0)

取消注释此行//checkTax.setOnClickListener(tax);并尝试使用onclicklistener或尝试使用

checkTax.setOnCheckedChangeListener(new OnCheckedChangeListener() {
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

   if(isChecked)
   {
    checkTax.setText("Check Box is checked");
   }
   else
   {
    checkTax.setText("not checked");
   }

});

我认为即使单击复选框

也会触发setOnCheckedChangeListener