Android checkbox.isChecked () not working properly

I have a ListView in which a checkbox displayed on every row.
Each time the checkbox touched, I check that it is checked or not.
But each time the first element always returns false . However, if If checked the second element, the .ischecked() method of the flag of the first element always returns true .
Here is my code :

 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; } } 

What should I do to solve this problem? Thank you very much in advance!

+5
source share
2 answers

You must use setOnCheckedChangeListener.

Example:

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

In your code:

  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"); } } }); 
+14
source

To use a checkbox in the form of Onclicklistener this will help you

 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(); } }); 
-1
source

Source: https://habr.com/ru/post/1216512/


All Articles