Jas
Jas

Reputation: 3212

Showing multiple AlertDialog one after another

Suppose I have an ArrayList of 4 string values. I want to display 4 AlertDialogs with these 4 string values as message. So my doubt is, I want to display one AlertDialog only after one is dismissed. So these should be displayed back to back when ok button/ cancel button is clicked. Any help will be greatly appreciated.

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();
    }

Upvotes: 0

Views: 2376

Answers (3)

Sathish Kumar J
Sathish Kumar J

Reputation: 4345

Try this,

 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();

}

call

myAlert(count);

this might helps you

Upvotes: 0

Sanoop Surendran
Sanoop Surendran

Reputation: 3486

First create a function with accepts the count and individual messages like and create a global variable 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();
   }

  }

Please try and let me know, and call the function from where you want to show the first dialog

Upvotes: 1

miskohut
miskohut

Reputation: 1027

You could do it in recursion.

    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();

        }

    }

I'm in work now so I cannot test this solution, but you can try :)

Upvotes: 0

Related Questions