2011-10-15 43 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 
     } 
    }