lundi 24 janvier 2022

How to get checkbox values in android studio with onCheckboxClicked

private CheckBox ch;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    register = findViewById(R.id.btn_register);
    ch = findViewById(R.id.checkboxCh);

    register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            JSONObject jsonObject = new JSONObject();
            RequestQueue requestQueue = Volley.newRequestQueue(RegisterActivity.this);

            try{
                jsonObject.put("ch", ch);
            }catch (JSONException exception){
                exception.printStackTrace();
            }

            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, REGISTER_URL, jsonObject, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    System.out.println(response);
                    Toast.makeText(RegisterActivity.this, "You have registered", Toast.LENGTH_LONG).show();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                    Toast.makeText(RegisterActivity.this, "An error occurred", Toast.LENGTH_SHORT).show();
                }
            });

            //add request to the requestQueue
            requestQueue.add(jsonObjectRequest);
        }
    });

}

public void onCheckboxClicked(View view){
    //Is the view now checked?
    Boolean checked = ((CheckBox) view).isChecked();

    //Check if checkbox is selected or not
    if(ch.isChecked()){
        //checked=true;
    }else{
        //checked=false;
    }
}

I am trying to send the true or false answer from boolean onCheckBoxClicked using JSONObject to my database. But whatever code I tried in if statement was wrong. Do you have any suggestions? I would apreciate it. Thank you!




Aucun commentaire:

Enregistrer un commentaire