2016-11-04 102 views
0

我在我的活动中显示了打开(如果为真)或关闭(如果为假)切换按钮。我实现了一个setOnCheckedChangeListener方法来更改我的数据库中切换按钮的状态。在Android中更改切换按钮状态的问题

在我的这部分代码我改变了切换按钮的状态,当有人点击:

mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     if (isChecked) { 

      AlertDialog.Builder alertDialog = new AlertDialog.Builder(TabLayoutScreenActivity.this); 
      alertDialog.setMessage("Have you updated today's menu?"); 
      alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int which) { 

        callServiceToOpenBistro(); 
        Toast.makeText(getApplicationContext(), "Your Bistro is now open", Toast.LENGTH_SHORT).show(); 
        OpenClose.setText("Open"); 
        mySwitch.setChecked(true); 
        dialog.cancel(); 
       } 
      }); 

      alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        Toast.makeText(getApplicationContext(), "Please updated in order to open your Bistro", Toast.LENGTH_SHORT).show(); 
        OpenClose.setText("Closed"); 
        mySwitch.setChecked(false); 
        dialog.cancel(); 
       } 
      }); 
      alertDialog.show(); 


     } else { 

      callServiceToOpenBistro(); 
      OpenClose.setText("Closed"); 

     } 
    } 
}); 

而且在我的代码这一部分,我在我的数据库检查有关的数据切换按钮存储在我的数据库是真的还是假的(打开或关闭):

private void callServiceToCheckStatus() { 

    new VolleyHelper(this).get("current_status/" + PrefernceHelper.getString(this, Commons.Constants.USER_ID), null, new Response.Listener<JSONObject>() { 
     @Override 
     public void onResponse(JSONObject response) { 
      try { 
       JSONArray jsonArray = response.getJSONArray("bistro_status"); 
       String status = "",flag = "",opHours = ""; 
       for (int i = 0; i < jsonArray.length(); i++) { 
        JSONObject jsonObject = jsonArray.getJSONObject(i); 

        status = jsonObject.getString("status"); 

       } 
       if (status.equals("Online")){ 
        OpenClose.setText("Open"); 
        mySwitch.setChecked(true); 

       } else if (status.equals("Offline")){ 
        OpenClose.setText("Closed"); 
        mySwitch.setChecked(false); 

       } 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 

     } 
    }); 
} 

的问题是,在callServiceToCheckStatus方法,让我的数据库中的地位后,我改变了切换按钮为true(如果它已经打开)或假(如果它存储关闭)。但是当我使用“setChecked”时,它会调用“setOnCheckedChangeListener”方法。这不是我真正想要的。

有没有办法以不同的方式做到这一点?在不调用setOnCheckedChangeListener的情况下更改切换按钮的状态,或者可能使用侦听器来检查切换是否被单击,而不是检查其状态是否已更改(setOnCheckedChangeListener)?

+0

的可能的复制[变化选择框的值,而不会触发onCheckChanged]再次装上监听器(http://stackoverflow.com/questions/15523157/change -checkbox-value-without-triggering-oncheckchanged) – cascal

回答

1

我认为this解决方案应该适合你。

您删除侦听器(setOnCheckedChangeListener (null);),调用mySwitch.setChecked(true or false)和(mySwitch.setOnCheckedChangeListener(reference of listener)

+0

我不知道我可以做到这一点。非常感谢你! –