2016-11-04 99 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

我不知道我可以做到這一點。非常感謝你! –