2016-06-28 59 views
0

假設我有一個ArrayList的4個字符串值。我想用這4個字符串值作爲消息顯示4 AlertDialogs。所以我的疑問是,我想在一個人被解僱後才顯示一個AlertDialog。所以當點擊確定按鈕/取消按鈕時,這些應該重新顯示。任何幫助將不勝感激。一個接一個地顯示多個AlertDialog

ArrayList<String> messages = new ArrayList<String>(); 
    messages.add("One"); 
    messages.add("Two"); 
    messages.add("Three"); 
    messages.add("Four"); 
    for(int i=0; i<messages.size(); i++) 
    { 
     AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this); 
     builder1.setMessage(messages.get(i)); 
     builder1.setCancelable(true); 

     builder1.setPositiveButton(
      "Yes", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 
       } 
      }); 

     builder1.setNegativeButton(
      "No", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 
       } 
      }); 

     AlertDialog alert11 = builder1.create(); 
     alert11.show(); 
    } 
+0

現在它必須完全顯示所有的對話? –

+0

是的。第四個將在上面和3,2,1將在它下面 – Jas

+0

@Jas請檢查我編輯的答案..哪些將顯示在4,3,2,1順序 – Sanoop

回答

0

你可以在遞歸中做到這一點。

public class MessagesHandler { 
     private ArrayList<String> messages = new ArrayList<String>(); 

     public MessagesHandler() { 
      messages.add("One"); 
      messages.add("Two"); 
      messages.add("Three"); 
      messages.add("Four"); 

      displayMessage(0); 
     } 

     public void displayMessage(int index) { 
      if (index < 0 || index >= messages.size()) { 
       return; 
      } 

      AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this); 
      builder1.setMessage(messages.get(index)); 
      builder1.setCancelable(true); 

      builder1.setPositiveButton("Yes", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 
        displayMessage(++index); 
       } 
      }); 

      builder1.setNegativeButton("No", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 
        displayMessage(++index); 
       } 
      }); 

      AlertDialog alert11 = builder1.create(); 
      alert11.show(); 

     } 

    } 

我現在的工作是如此我不能對此進行測試的解決方案,但你可以試試:)

1

首先創建一個函數接受計數和個人信息等,並創建一個全局變量int count = 0

private void buildAlertDialog(int length, String message) { 
    if (count != lenght) { 
    AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this); 
    builder1.setMessage(message); 
    builder1.setCancelable(true); 

    builder1.setPositiveButton(
     "Yes", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.cancel(); 
       count++; 
       buildAlertDialog(messages.size(), message.get(lenght-count+1)); 
      } 
     }); 

    builder1.setNegativeButton(
     "No", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.cancel(); 
       count++; 
       buildAlertDialog(messages.size(), message.get(lenght-count+1)); 
      } 
     }); 

    AlertDialog alert11 = builder1.create(); 
    alert11.show(); 
    } 

    } 

請嘗試,讓我知道,並要求從那裏你要顯示的第一dialog

0

試試這個功能,

int count = 0; 

public void myAlert(int index) 
{ 

AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this); 
    builder1.setMessage(messages.get(count)); 
    builder1.setCancelable(true); 

    builder1.setPositiveButton(
     "Yes", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.cancel(); 
       if(count != messages.size()) 
       { 
        count++; 
        myAlert(count); 
       } 
      } 
     }); 

    builder1.setNegativeButton(
     "No", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.cancel(); 
      } 
     }); 

    AlertDialog alert11 = builder1.create(); 
    alert11.show(); 

} 

呼叫

myAlert(count); 

這種力量可以幫助你