tags:

views:

64

answers:

1

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;
                }
            }
A: 

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.

I82Much
Not exactly what I need but it got me thinking and I think I can come up with a solution using a similar method. I'll try to calculate most of it then wait to print results until after the listener is called.
Fsmv
Thank you for your blog post. It shows that you can't pause the program and why. So I choose a different approach and it works now!
Fsmv
Excellent - what was the approach you chose?
I82Much