Вызов того же AlertDialog в AlertDialog - PullRequest
0 голосов
/ 15 октября 2011

У меня есть 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;

        }
    }
}

Может кто-нибудь помочь?

1 Ответ

0 голосов
/ 18 ноября 2011

разобрался с ответом так долго!В методе 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
       }
    }
...