2012-02-18 95 views
3

我想在另一個AlertDialog中打開一個AlertDialog,但它不工作,爲什麼它不工作的任何想法?AlertDialog.Builder打開另一個AlertDialog.Builder

String items[] = {"Details","Edit","Delete"} 
AlertDialog.Builder alert = new AlertDialog.Builder(getAplicationContext()); 
alert.setTitle("Options"); 
alert.setItems(items, new OnClickListener() { 

    public void onClick(DialogInterface dialog, int item) { 
     switch(item){ 
      case 0: 
       AlertDialog.Builder alert2 = new AlertDialog.Builder(getAplicationContext()); 
       alert2.setTitle("Details"); 
       alert2.setMessage(getDetails()); 
       alert2.setNeutralButton("Close", null); 
       alert2.show(); 
      return; 

      case 1: 
       //not important for the question 
      return; 

      case 2: 
       //not important for the question 
      return; 
     } 
    } 
}); 

alert.setNegativeButton("Cancel", null); 
alert.show(); 
+2

定義「不工作」。嘗試在另一個AlertDialog中打開AlertDialog時是否收到任何錯誤消息?謝謝你, – Rob 2012-02-18 17:24:22

回答

6

該問題可能是您用於AlertDialog的環境。嘗試在兩者中使用MyActivityName.this,用您的Activity的名稱替換MyActivityName。

因此,建設的第一個AlertDialog應該是這樣的

AlertDialog.Builder alert = new AlertDialog.Builder(MyActivityName.this);

然後

AlertDialog.Builder alert2 = new AlertDialog.Builder(MyActivityName.this);

的第二個。

+0

真的很有用。現在工作 – 2012-02-19 12:53:09

+0

@FelipeSuman在這些論壇上習慣於接受答案(點擊旁邊的複選標記)找到答案。謝謝,祝你好運! – koopaking3 2012-03-06 02:21:31

+0

它幫助我解決了類似的問題,需要另一個upvote – 2012-11-26 20:39:02