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 に答える