-3
\$\begingroup\$

I am trying to save a radiobutton string if it clicked. I am making a survey where I need to save a "yes" or "no" into a MYSQL table using volley. I am having a little trouble doing this and have tried to use the method below. Does anyone have any insight on how to do this? Right now I am

Please find my code below:

private void MCIRadioSave() {
        GoToMain.setVisibility(View.VISIBLE);
        GoToJournal.setVisibility(View.VISIBLE);
        GoToMain.setVisibility(View.GONE);
        GoToJournal.setVisibility(View.GONE);
        if(yesBTN.isChecked())
        {
            final String yes1 = this.yesBTN.getText().toString().trim();
        }
        else
        {
            final String no1 = this.noBTN.getText().toString().trim();
        }
        if(yesBTN2.isChecked())
        {
            final String yes2 = this.yesBTN2.getText().toString().trim();
        }
        else
        {
            final String no2 = this.noBTN2.getText().toString().trim();
        }
        if(yesBTN3.isChecked())
        {
            final String yes3 = this.yesBTN3.getText().toString().trim();
        }
        else
        {
            final String no3 = this.noBTN3.getText().toString().trim();
        }
        if(yesBTN4.isChecked())
        {
            final String yes4 = this.yesBTN4.getText().toString().trim();
        }
        else
        {
            final String no4 = this.noBTN4.getText().toString().trim();
        }
        if(yesBTN5.isChecked())
        {
            final String yes5 = this.yesBTN5.getText().toString().trim();
        }
        else
        {
            final String no5 = this.noBTN5.getText().toString().trim();
        }
        if(yesBTN6.isChecked())
        {
            final String yes6 = this.yesBTN6.getText().toString().trim();
        }
        else
        {
            final String no6 = this.noBTN6.getText().toString().trim();
        }
        if(yesBTN7.isChecked())
        {
            final String yes7 = this.yesBTN7.getText().toString().trim();
        }
        else
        {
            final String no7 = this.noBTN7.getText().toString().trim();
        }
        if(yesBTN8.isChecked())
        {
            final String yes8 = this.yesBTN8.getText().toString().trim();
        }
        else
        {
            final String no8 = this.noBTN8.getText().toString().trim();
        }



        StringRequest stringRequest = new StringRequest(Request.Method.POST, MCI_SAVE,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            String success = jsonObject.getString("success");

                            if (success.equals("1")) {
                                Toast.makeText(checkin1.this, "Thank you for completing your check in", Toast.LENGTH_SHORT).show();
                                GoToMain.setVisibility(View.VISIBLE);
                                GoToJournal.setVisibility(View.VISIBLE);
                            } else {
                                Toast.makeText(checkin1.this, "Register Failed", Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(checkin1.this, LoginActivity.class));
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(checkin1.this, "Register Error" + e.toString(), Toast.LENGTH_SHORT).show();
                            GoToMain.setVisibility(View.VISIBLE);
                            GoToJournal.setVisibility(View.VISIBLE);
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(checkin1.this, "Register Error " + error.toString(), Toast.LENGTH_SHORT).show();
                        Log.e("Gallery error =>", error.toString());
                        GoToMain.setVisibility(View.VISIBLE);
                        GoToJournal.setVisibility(View.VISIBLE);
                    }
                }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("Q1Medical", yes1 || no1 );
                params.put("Q2Safety",  no2 || yes2 );
                params.put("Q3Completing",  no3 || yes3 );
                params.put("Q4TP", no4 || yes4 );
                params.put("Q5Supervisor",  no5 || yes5 );
                params.put("Q6English",  no5 || yes5 );
                params.put("Q7Hours",  no6 || yes6 );
                params.put("Q8Additional",  no7 || yes7 );
                params.put("Q8Additional",  no8 || yes8 );

                return params;
            }
        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    } ```
\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome to Code Review! It sounds like this code isn't working to the best of your knowledge, which would not be in line with what is on-topic here. When you have implemented the code feel free to edit your post to include it for a review. \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ Jul 17 '20 at 17:43
  • \$\begingroup\$ (save a radiobutton string when the button is clicked?) \$\endgroup\$ – greybeard Jul 18 '20 at 7:02
-1
\$\begingroup\$

Your yesX / noX variables are local only to the if else block you declared them in.

final String foo;
if (yesBTN4.isChecked()) {
  foo = "bar";
} else if(noBTN4.isChecked()) {
  foo = "barz";
}

Which brings me to another choice you decided to make. How are you going to determine if you're passing yes2 or no2 into your database. That would require another check. Instead have one variable to store per question response. Noticed how I used one variable for both yes and no and it equals different things based on branch.

Finally, there is no reason to call trim() on the the radiobutton .getText() because the text is typically what YOU specified it to be. So unless you are doing something crazy like RadioButton rb5 = new RadioButton(" Hello, World! "); there isn't a reason to make that method call.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.