0

我有一个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;

        }
    }
}

任何人都可以帮忙吗?

4

1 回答 1

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
       }
    }
于 2011-11-18T18:28:11.980 回答