2012-01-18 1 views
1

私は自分のアプリケーションで自分のコードを最適化しようとしています。私はどのように進んでいないので、真剣にいくつかの助けを使うことができます。ダイアログボックスコードの最適化

私は警告ダイアログボックスを作成しています、と私はこれを作成するために管理しているGoogleの例を読んだ後、レイアウト

private void doScript2() { 
    AlertDialog.Builder alert_box=new AlertDialog.Builder(this); 
    alert_box.setIcon(R.drawable.caution); 
    alert_box.setTitle("Caution"); 
    alert_box.setMessage("Mount external sd as NTFS at boot?"); 
    alert_box.setPositiveButton("Yes",new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface dialog, int which) { 
      Toast.makeText(getApplicationContext(), "External SD will be mounted",Toast.LENGTH_SHORT).show(); 
      try { 
       .... 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } catch (RootToolsException e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
    alert_box.setNegativeButton("No", new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface dialog, int which) { 
     } 
    }); 
    alert_box.show(); 
} 

としてこれを使用するために使用しています。

private static final int DIALOG_TWRESTORE = 202; 
private static final int DIALOG_TWRESTART = 204; 
private static final int DIALOG_TWONESIX = 205; 
private static final int DIALOG_TWONENINE = 206; 
private static final int DIALOG_TWTWOFOUR = 207; 

protected Dialog onCreateDialog(int id) { 
    switch (id) { 
    case DIALOG_TWRESTORE: 
     return createDialog("Restore", "Are you sure you want to restore your TW?"); 
    case DIALOG_TWRESTART: 
     return createDialog("Restart", "Restart TW?"); 
    case DIALOG_TWONESIX: 
     return createDialog("Resize to 6x6", "Are you blablabla of TW?"); 
    case DIALOG_TWONENINE: 
     return createDialog("Resize to 5x5", "Are you on blablabla of TW?"); 
    case DIALOG_TWTWOFOUR: 
     return createDialog("Resize to 4x5", "Are you on blablabla of TW?"); 
    default: 
     return super.onCreateDialog(id); 
    } 
} 

private Dialog createDialog(String title, String message) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle(title) 
    .setIcon(R.drawable.question) 
    .setMessage(message) 
    .setCancelable(false) 
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      //I want to pass on a parameter from the above class to use within the dialog 
      **parametergoeshere*();    
     } 
    }) 
    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
     } }); 
    return builder.create(); 
} 

私の主な質問は、私は[はい]ボタンをクリックする時に異なる何かをしたいはい/いいえダイアログボックスを作っていますということです。私はパラメータと同様の方法でこれを渡したいので、私が使用したコードを繰り返さないでください。これを行う方法はありますか?ありがとうございます。

編集:

次のコードは動作しませんでした。私は取得しています

protected Dialog onCreateDialog(int id) { 
    switch (id) { 
    case DIALOG_TWRESTORE: 
     return createDialog("Restore", "Are you sure you want to restore your TW?", Commands.doTWBackup()); 
    default: 
     return super.onCreateDialog(id); 
    } 
} 

private Dialog createDialog(String title, String message, Object param) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle(title) 
    .setIcon(R.drawable.question) 
    .setMessage(message) 
    .setCancelable(false) 
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      //I want to pass on a parameter from the above class to use within the dialog 
      param;    
     } 
    }) 
    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
     } }); 
    return builder.create(); 
} 

エラーは以下のとおりです。

Syntax error, insert "AssignmentOperator Expression" to complete Expression 

The method createDialog(String, String, Object) in the type Launcher is not applicable for the arguments (String, String, void) 

答えて

0

あなたはonCreateDialog機能で[はい]ボタン用DialogInterface.OnClickListenerオブジェクトを定義する必要があり、その後、パラメータとしてcreateDialogを関数に渡しますここでそれを使用して新しいダイアログを作成します。

+0

私はそれをやってみましたが、あなたが見ることができるように私の答えを編集させてください。 –

0

私はこれをやってしまった最良の方法は以下の通りです:

protected Dialog onCreateDialog(int id) { 
    switch (id) { 
    case DIALOG_RIGHTTOLEFT: 
     return new AlertDialog.Builder(this).setTitle("Caution").setIcon(android.R.drawable.ic_dialog_alert) 
       .setMessage("Do you need to apply Right to Left Patch? This is NOT Revertable!") 
       .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         RootCommands.doRightToLeft(); 
         Toast.makeText(getApplicationContext(), "RTL Patch Applied. Please Reboot.",Toast.LENGTH_SHORT).show(); 
        }}).setNegativeButton(android.R.string.no, null).create(); 
    case DIALOG_MOUNTNTFS: 
     return new AlertDialog.Builder(this).setTitle("Caution").setIcon(android.R.drawable.ic_dialog_alert) 
       .setMessage("This will mount your external SDCard as NTFS and Break apps2sd. Continue?") 
       .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         RootCommands.doMountNTFS(); 
         Toast.makeText(getApplicationContext(), "External SD Mounted as NTFS",Toast.LENGTH_SHORT).show(); 
        }}).setNegativeButton(android.R.string.no, null).create(); 
    case DIALOG_MOUNTONBOOT: 
     return new AlertDialog.Builder(this).setTitle("Caution").setIcon(android.R.drawable.ic_dialog_alert) 
       .setMessage("Mount external sd as NTFS at boot? This might cause issues.") 
       .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         RootCommands.doMountOnBoot(); 
         Toast.makeText(getApplicationContext(), "Reboot to take effect.",Toast.LENGTH_SHORT).show(); 
        }}).setNegativeButton(android.R.string.no, null).create(); 
    default: 
     return super.onCreateDialog(id); 
    } 

質問はまだ適用されます。誰かがこれに対してよりよい解決策を選ぶならば、分かち合いましょう。