0

I hope someone help me for this. I need to implement a system of "cascading" alert dialogs. The user inserts a serie of information like as a flow, passing from a dialog to one other, storing data in an array. If a certain condition is verified, at the end of this flow i store array's data into a database.

Here is my code that could implement this:

this is the cycle which implements the flow, launched by an onMenuItemClick event

count=0;
do{
        AlertDialog dialog;
        dialog = alertSequence(view,label,edit);
        dialog.show();
}while(count<4);

and this is the alertSequence() function

public AlertDialog alertSequence(View v,TextView t,EditText e){
    AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
    final EditText edit = e;
    switch(count){
    case 0:
        builder.setView(v);
        t.setText("Inserisci il titolo per il nuovo canto");
        builder.setPositiveButton("Continua", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                String elem = edit.getText().toString().trim();
                if(elem.equals(null) || elem.length()==0){
                    // non faccio nulla
                }
                else{
                    info[count] = elem;
                    count++;
                }
            }
            }).setNegativeButton("Annulla", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    count = 10;
                    dialog.cancel();
                }
            });
        break;
    case 1:
        builder.setView(v);
        t.setText("Inserisci l'autore per il nuovo canto");
        builder.setPositiveButton("Continua", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                String elem = edit.getText().toString().trim();
                if(elem.equals(null) || elem.length()==0){
                    // non faccio nulla
                }
                else{
                    info[count] = elem;
                    count++;
                }
            }
            }).setNegativeButton("Annulla", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    count = 10;
                    dialog.cancel();
                }
            });
        break;
    case 2:
        builder.setMessage("Seleziona la categoria da assegnare");
        int i=1;
        String query = "SELECT titolo FROM categorie WHERE titolo<>'uncategorized' ORDER BY titolo ASC";
        Cursor c = mydb.select(query);
        if(!c.equals(null)){
            options = new String[c.getCount()+1];
            options[0] = "(annulla)";
            while(c.moveToNext()){
                options[i] = c.getString(0);
                i++;
            }
        }
        c.close();
        mydb.close();
        builder.setCancelable(false);
        builder.setItems(options, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                if(options[which].equals("(annulla)"))
                    count = 10;
                else{
                    info[count] = options[which];
                    count++;
                }
            }
        });
        break;
    case 3:
        builder.setView(v);
        t.setText("Inserisci il testo per il nuovo canto");
        edit.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
        edit.setHeight((int) 300d);
        edit.setGravity(Gravity.LEFT | Gravity.TOP);
        edit.setHorizontallyScrolling(false);
        builder.setPositiveButton("Continua", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                String elem = edit.getText().toString().trim();
                if(elem.equals(null) || elem.length()==0){
                    // non faccio nulla
                }
                else{
                    info[count] = elem;
                    count++;
                }
            }
            }).setNegativeButton("Annulla", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    count = 10;
                    dialog.cancel();
                }
            });
        break;
    }

    return builder.create();
}

Is it correct for my scope? it is only that this generated a WindowManager$BadTokenException - Unable to add window token null is not for an application when i call the dialog.show() in the do-while cycle. What could be? Thanks for help.

EDIT:
thanks blackbelt, i resolved it. But now there's another error, Illegalstateexception - The specified child already has a parent android you must call removeView() from on the child's parent first on dialog.show(). I have red something of this problem but i don't understand how resolve it. Any suggestions?

Salvatore Ucchino
  • 717
  • 1
  • 8
  • 17
  • refer this [link](http://stackoverflow.com/questions/17341897/how-to-make-alert-with-a-alert-in-fragment/17342479#17342479) – MohsinSyd Jun 28 '13 at 09:16

1 Answers1

1

you get that exception because the context you need to create the Dialog is the Activity's one

AlertDialog.Builder builder = new AlertDialog.Builder(this); 

or

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

if you are inside a fragment

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • thanks blackbelt, i resolved it. But now there's another error, **Illegalstateexception - The specified child already has a parent android you must call** `removeView()` **from on the child's parent first** on `dialog.show()`. I have red something of this problem but i don't understand how resolve it. Any suggestions? – Salvatore Ucchino Jun 28 '13 at 09:49
  • i removed `AlertDialog.Builder builder` on top of `alertSequence()`and did this: `case 0: AlertDialog.Builder builder0 = new AlertDialog.Builder(this); ... case 1: AlertDialog.Builder builder1 = new AlertDialog.Builder(this); ...` but it doesn't work! It gives me the same error. The function `removeView()` where could be declared? Maybe the problem can be the custom view that i set to the dialog? Sorry for my inexperience, i'm a beginner.. – Salvatore Ucchino Jun 28 '13 at 10:20
  • sorry only now I realize you are calling it in a loop. Can you describe what do you want achieve. Maybe there is a better way – Blackbelt Jun 28 '13 at 10:26
  • of course. these alert dialogs are like input masks in which user inserts some input in sequence. after each dialog, its relative input is stored into an array firstly, and then, when the cycle ends, all of input are finally stored into a database. the condition of the cycle is a counter which increments itself if only i click on positive button and "pass" to the other dialog. i hope i explain it better – Salvatore Ucchino Jun 28 '13 at 10:37
  • more or less. Do you understand that dialog.show() is not a blocking call ? – Blackbelt Jun 28 '13 at 10:41
  • i found [here](http://stackoverflow.com/questions/7644592/how-to-make-alertdialog-block-the-code) a discussion. it means that i only call each dialog from the positive button event click of the prev dialog? – Salvatore Ucchino Jun 28 '13 at 11:00
  • I think you are expecting is the loop is waiting for you to press the positive button before go through the next iteration, am I right? – Blackbelt Jun 28 '13 at 11:02
  • yes, and this is wrong. instead, i could use a method for each dialog and call it following the order in which them must be called. What do you think? – Salvatore Ucchino Jun 28 '13 at 11:27
  • you can use the same method. register the [OnDismissListener](http://developer.android.com/reference/android/content/DialogInterface.OnDismissListener.html), and if the value of count changes call alertSequence again – Blackbelt Jun 28 '13 at 12:19