2012-07-17 52 views
0

我有很多不同的狀態,我需要以AlertDialog的形式向用戶反饋信息。爲每個警報創建一個單獨的類是瘋狂的。我現在擁有的是:如果您不想爲AlertDialogs創建十幾個類,該怎麼辦?

class FeedbackAlertDialog extends DialogFragment { 
    private String message; 
    private int action; 

    FeedbackAlertDialog() { 
    } 

    FeedbackAlertDialog(String message, int action) { 
     this.message = message; 
     this.action = action; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return super.onCreateView(inflater, container, savedInstanceState); 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     return new AlertDialog.Builder(getActivity()) 
       .setCancelable(false) 
       .setTitle(message) 
       .setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         switch (action) { 
          case action: // It's impossible because the int should be final 
         } 
         startActivity(new Intent(getActivity(), MainActivity.class)); 
        } 
       }).show(); 
    } 
} 

的問題是,我不能使用switch因爲int應該是最終的。如何處理這種情況?

+0

我認爲你需要**私人詮釋行動; **作爲最終? – 2012-07-17 14:50:47

+0

但是,如果我想'最後'我不能在'構造函數'中改變它。那就是問題所在。 – Eugene 2012-07-17 15:09:02

回答

0

這是隻因爲你需要而不可能在開關中使用常量。

+0

你出錯了!看到我更新的答案。 – 2012-07-18 17:52:03

0

使用:

FeedbackAlertDialog.this.action

至於我可以看到,這應該是在開關。

這種方式用於訪問更高級別的變量(通知設置人員在一個簡單的模型中)。

在您的情況下,您必須首先進入根對象的範圍(您的情況爲FeedbackAlertDialog)。

+0

編譯器要求「動作」保持不變。 – Eugene 2012-07-17 15:08:09

0

編輯::

在您的計算機系統試試這個簡單的Java演示:

public class CalcDemo { 
    public static void main(String[] args) { 
     int n1 = 6, n2 = 3; 
     int opr = 1; 
     MyMath math = new MyMath(n1, n2, opr); 
     System.out.println("Answer :: "+math.getResult()); 
    } 
} 

class MyMath { 
    int n1, n2, opr; 

    public MyMath() { } 
    public MyMath(int n1, int n2, int opr) { 
     this.n1 = n1; 
     this.n2 = n2; 
     this.opr = opr; 
    } 
    public int getResult() { 
     //int ch = opr; 
     switch (opr) { 
     case 1: return n1-n2; 
      //break; 
     case 2: return n1+n2; 
     default : System.out.println("Invalid Choice"); 
      break; 
     } 
     return 0; 
    } 
} 

做一個招數如下:

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    return new AlertDialog.Builder(getActivity()) 
      .setCancelable(false) 
      .setTitle(message) 
      .setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        final int action1 = action; // <---- --- Try This 
        switch (action1) { // <--- --- Use as This 
         case action1: // I THINK, NOW THIS IS POSSIBLE 
        } 
        startActivity(new Intent(getActivity(), MainActivity.class)); 
       } 
      }).show(); 
} 
+0

試過這個代碼? – 2012-07-17 15:25:20

+0

我做了,這也不起作用。 – Eugene 2012-07-17 15:28:35

+0

什麼是錯誤或異常? – 2012-07-17 15:29:33

相關問題