Fsmv
Fsmv

Reputation: 1176

AlertDialog doesn't wait for input

I have an AlertDialog created within a switch case statement which is inside a for loop. On the AlertDialog there is an EditText for input. When the box pops up the for loop is run through in the background. I want the loop to wait until input is sent to continue. Here is the code for the AlertDialog:

for(int i=0; i < code.length(); i++){
        switch(code.charAt(i)){
                case '+':
                    bytes[index]++;
                    break;
                case '-':
                    bytes[index]--;
                    break;
                case '<':
                    if(index > 0){
                        index--;
                    }else{
                        Toast.makeText(this, "Warning: Index is already at zero", Toast.LENGTH_LONG).show();
                    }
                    break;
                case '>':
                    if(index <= 500){
                        index++;
                    }else{
                        Toast.makeText(this, "Warning: Maximum bytes reached", Toast.LENGTH_LONG).show();
                    }
                    break;
                case ']':
                    if(loop == -1){
                        Toast.makeText(this, "ERROR: Close bracket before an open bracket!", Toast.LENGTH_LONG).show();
                        errors++;
                        break;
                    }else{
                        if(bytes[index] == 0){
                            loop = -1;
                        }else{
                            i = loop;
                        }
                        break;
                    }
                case '[':
                    loop = i;
                    break;
                case '.':
                    stdout(Character.toString((char)bytes[index]));
                    break;
                case ',':
                    AlertDialog.Builder alert = new AlertDialog.Builder(this).setCancelable(false);

                    alert.setTitle("Enter Character");
                    alert.setMessage("Script is requesting input");

                    final EditText input = new EditText(this);
                    InputFilter[] FilterArray = new InputFilter[1];
                    FilterArray[0] = new InputFilter.LengthFilter(1);
                    input.setFilters(FilterArray);

                    alert.setView(input);

                    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            setByte(input.getText().toString().charAt(0));
                        }
                   });

                   alert.show();
                   break;
                }
                if(errors > 0){
                    break;
                }
            }

Upvotes: 1

Views: 14115

Answers (1)

I82Much
I82Much

Reputation: 27326

As others have said, the Android dialog boxes are not modal (which is to say, they don't block the background processes). Read my blog post for a tutorial on how you get around this, by registering callbacks.

Upvotes: 8

Related Questions