Switch isChecked boolean Android

I have a switch as shown below.

Create League

I enter the League’s name,and a league is created via a web service.

The switch code is.

aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    openLeague="OPEN";
                }else{
                    openLeague = "CLOSED";
                }
            }
        });

The button code…

 btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                session = new SessionManager(getApplicationContext());
                leagueName = teamTextField.getText().toString();


                if (!leagueName.isEmpty()) {

                    createLeague(leagueName, username, password, start, end, openLeague);

                } else {
                    Toast.makeText(getApplicationContext(),
                            "Please enter your details!", Toast.LENGTH_LONG)
                            .show();
                }
            }
        });

The createLeague(…) contains the web service. I am posting six variables. The one I am interested is the open_league.

private void createLeague(final String leagueName, final String username, final String password,final String start,final String end,final String openLeague) {
    String tag_json_obj = "json_obj_req";



    final HashMap<String, String> postParams = new HashMap<String, String>();

    postParams.put("league_name",leagueName);

    postParams.put("username",username);
    postParams.put("password",password);
    postParams.put("league_start",start);

    postParams.put("league_finish",end);
    postParams.put("open_league",openLeague);

    Response.Listener<JSONObject>  listener;
    Response.ErrorListener errorListener;
    final JSONObject jsonObject = new JSONObject(postParams);

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(AppConfig.URL_CREATE_LEAGUE, jsonObject,
            new com.android.volley.Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d("TAG", response.toString());
                    try {

                        if (response.getString("status").equals("success")){

                            Intent i = new Intent(CreateLeague.this, League.class);
                            startActivity(i);
                            finish();

                        }
                    } catch (JSONException e) {
                        Log.e("TAG", e.toString());
                    }
                    //pDialog.dismiss();
                }
            }, new com.android.volley.Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            //VolleyLog.d("TAG", "Error: " + error.getMessage());
            //pDialog.dismiss();

        }
    }) {

        @Override
        public String getBodyContentType() {
            return "application/json; charset=utf-8";
        }


    };

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
    // VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjRequest);
}

I put a breakpoint before this line to check the json I am sending. So when the switch is open I get.

{
 "league_start":"28-07-2015 09:37:43",
 "username":"cerberus_30@gmail.com",
 "league_finish":"11-08-2015 09:37:43",
 "league_name":"Samsung League",
 "password":"larissa",
 "open_league":"OPEN"
}

You see the open_league json variable is open. However when the switch is closed,the open_league variable is null! Why is this? I have put in my switch listener the open_league to be “OPEN” when it is on when CLOSED when it is OFF. Thanks.


Source: android

Leave a Reply