2011-10-15 3 views
0

J'ai un AlertDialog qui invite l'utilisateur s'il veut envoyer des données. Ce que je fais, c'est que je vérifie s'il y a une connexion Internet, sinon je vais afficher à nouveau la boîte de dialogue. La boîte de dialogue s'affiche, mais lorsque je clique sur 'Oui', la boîte de dialogue n'apparaît pas lorsque la connexion est coupée.Appeler le même AlertDialog dans le 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; 

     } 
    } 
} 

Quelqu'un peut-il aider?

Répondre

0

Imaginez la réponse après si longtemps! Dans la méthode sendData(), au lieu d'appeler showDialog(), vous devez reconstruire la boîte de dialogue

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 
     } 
    }