【问题标题】:Android checkbox.isChecked() does not work properlyAndroid checkbox.isChecked() 无法正常工作
【发布时间】:2015-06-04 11:51:47
【问题描述】:

我有一个ListView,其中每行显示一个checkbox
每次触摸checkbox 时,我都会检查它是否被选中。
但每次,第一项总是返回false。但是,如果选中第二项,则第一项复选框的.ischecked() 方法总是返回true
这是我的代码

public class CustomSpecificCar extends ArrayAdapter<JSONObject>{
    ListSpecificCars caller;
    private JSONObject currentJson;
    private CheckBox cbSelectedCar;
    private int position;

    public CustomSpecificCar(Context ctxt, List<JSONObject> list, ListSpecificCars caller){
        super(ctxt, R.layout.custom_specific_car_row, list);
        this.caller = caller;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View customView = inflater.inflate(R.layout.custom_specific_car_row, parent, false);
        this.position = position;

        // Set the reference of the layout
        currentJson                       = getItem(this.position);
        cbSelectedCar                     = (CheckBox)customView.findViewById(R.id.cbSelectedCar);
        TextView tvBrand                  = (TextView)customView.findViewById(R.id.tvBrand);
        TextView tvModel                  = (TextView)customView.findViewById(R.id.tvModel);
        TextView tvOwnerEditable          = (TextView)customView.findViewById(R.id.tvOwnerEditable);
        TextView tvPriceEditable          = (TextView)customView.findViewById(R.id.tvEstimatedPriceEditable);


        try {
            tvBrand.setText(currentJson.getString("brand"));
            tvModel.setText(currentJson.getString("model"));
            tvOwnerEditable.setText(currentJson.getString("owner"));
        } catch (JSONException e) {
            Log.e(e.getClass().getName(), "JSONException", e);
        }

        cbSelectedCar.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                    if (cbSelectedCar.isChecked()){
                        caller.updateClickedUsername(currentJson, true); // Add to the List
                        Log.d("Added click ", "ok");
                    }

                    else if (!cbSelectedCar.isChecked()) {
                        caller.updateClickedUsername(currentJson, false); // Delete from the List
                        Log.d("Deleted click ", "nok");
                    }
        }});

        return customView;
    }

}

我应该怎么做才能解决这个问题? 非常感谢!

【问题讨论】:

    标签: java android checkbox


    【解决方案1】:

    对于使用复选框作为 Onclicklistener 这将帮助你

    CheckBox.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                   Toast.makeText(this, "CheckBox is checked", Toast.LENGTH_SHORT).show();
                   } else {
                   Toast.makeText(this, "CheckBox is not checked", Toast.LENGTH_SHORT).show();
                }
        });
    

    【讨论】:

      【解决方案2】:

      你应该使用 setOnCheckedChangeListener。

      示例:

        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      
              @Override
              public void onCheckedChanged(CompoundButton buttonView,
                  boolean isChecked) {
      
              }
            });
      

      在您的代码中:

          cbSelectedCar.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      
              @Override
              public void onCheckedChanged(CompoundButton buttonView,
                  boolean isChecked) {
                    if (isChecked){
                          caller.updateClickedUsername(currentJson, true); // Add to the List
                          Log.d("Added click ", "ok");
                      }
      
                      else if (!isChecked) {
                          caller.updateClickedUsername(currentJson, false); // Delete from the List
                          Log.d("Deleted click ", "nok");
                      }
      
              }
            });
      

      【讨论】:

      • 欢迎您。不要忘记为答案投票,然后其他人可以将其视为“解决方案”
      • 好兄弟!
      猜你喜欢
      • 2017-03-10
      • 2012-09-13
      • 2012-03-24
      • 2012-11-03
      • 2013-11-24
      • 1970-01-01
      • 2014-09-17
      • 2011-07-04
      • 2015-10-04
      相关资源
      最近更新 更多