2011-10-15 38 views
0

我有一個AlertDialog提示用戶是否想要發送數據。我正在做的是檢查是否有互聯網連接,如果沒有,我會再次顯示對話框。 對話框會顯示,但是當我點擊「是」時,連接關閉時它不會顯示相同的對話框。在AlertDialog中調用相同的AlertDialog

public void sendData(){ 
    boolean connected = checkConnectivity(getApplicationContext()); 
    //connected is false, but dialog doesnt show the second time. 

      if(connected==false){ 
       //show dialog 
       showDialog(0); 
      }else{ 
       //connected, send data 
      } 
     } 

@Override 
protected Dialog onCreateDialog(int id) 
{ 

     return 
    new AlertDialog.Builder(this) 
     .setTitle("Send data?") 
     .setPositiveButton("Yes", new DialogButtonClickHandler()) 
     .setNegativeButton("No", new DialogButtonClickHandler()) 
     .create(); 

} 

public class DialogButtonClickHandler implements DialogInterface.OnClickListener 
{ 
    public void onClick(DialogInterface dialog, int clicked) 
    { 

     switch(clicked) 
     { 
      case DialogInterface.BUTTON_POSITIVE: 
       //Problem occurs here. sendData() gets called but dialog not displayed the second time 
          sendData(); 
       break; 
      case DialogInterface.BUTTON_NEGATIVE: 
       return; 

     } 
    } 
} 

任何人都可以幫忙嗎?

回答

0

經過這麼長的時間才找出答案!在sendData()方法中,不是調用showDialog(),而是需要重新生成對話框

public void sendData(){ 
boolean connected = checkConnectivity(getApplicationContext()); 
//connected is false, but dialog doesnt show the second time. 

     if(connected==false){ 
      //rebuild and show dialog 
      AlertDialog newDialog = new AlertDialog.Builder(this) 
      .setTitle("Send data?") 
      .setPositiveButton("Yes", new DialogButtonClickHandler()) 
      .setNegativeButton("No", new DialogButtonClickHandler()) 
      .create(); 
      newDialog.show(); 


     }else{ 
      //connected, send data 
     } 
    }