簡體   English   中英

Android checkbox.isChecked()無法正常運行

[英]Android checkbox.isChecked() does not work properly

我有一個ListView ,其中每行顯示一個checkbox
每次觸摸checkbox ,我都會檢查是否檢查了checkbox
但每次,第一項始終返回false 但是,如果選中第二個項目,則第一個項目的復選框的.ischecked()方法.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;
    }

}

我該怎么做才能解決這個問題? 提前謝謝了!

您應該使用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");
                }

        }
      });

對於使用復選框作為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();
            }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM