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() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    openLeague = "CLOSED";

The button code…

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

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

                if (!leagueName.isEmpty()) {

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

                } else {
                            "Please enter your details!", Toast.LENGTH_LONG)

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>();




    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>() {

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

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

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

    }) {

        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",
 "league_finish":"11-08-2015 09:37:43",
 "league_name":"Samsung League",

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.