0

I am trying to write an app that shows the user a string on textview, and replace the text due to user input. This should happen on time intervals of 60 seconds. I tried to use a countdown timer, which sets a boolean var to false on it's onFinish method. While this boolean is true, I am trying to change the strings in the textview (further work will be to change the text due to the user actions, but I'm still trying to get this work for simple actions).

For now, the app seems to be stucked and nothing happens, but if I am removing the while loop, there is a single text on the screen (as it should be).

Is there a problem using a while loop for that purpose? Is there a different way to work along with the timer?

(p.s I know there are some problems in the code, but this is just for isolating the problem. Thanks!)

EDIT: I will try to clarify my intentions: I want to give the user tasks, which he should perform in a given time. I want to display the time remaining on the screen, along with the task to perform. When the user finishes the task, if there is still time on the clock, he will press a button and another task will appear. The goal is to finish as many tasks as possible in the given time.

My code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start_game);

    Bundle b = getIntent().getExtras();
    game = b.getParcelable("game_record");

    figuresList = new ArrayList<String>(game.getFiguresList());

    clockView = (TextView) findViewById(R.id.clockView);
    figureText = (TextView) findViewById(R.id.figureText);

    timer = new CountDownTimer(10000, 1000) {

        public void onTick(long millisUntilFinished) {
            clockView.setText(Long.toString(millisUntilFinished / 1000));
        }

        public void onFinish() {
            clockView.setText("done!");
            isTurn = false;
        }
    };

    playRound();
}

private void playRound() {

    figuresIterator = figuresListUsage.iterator();
    isTurn = true;
    String nextFigure = figuresIterator.next();
    timer.start();

    while (isTurn == true) {
        figureText.setText(nextFigure);
        nextFigure = figuresIterator.next();
    }
}
orizis
  • 166
  • 2
  • 15
  • Remove the loop and add `figureText.setText(nextFigure); nextFigure = figuresIterator.next();` to your `onTick()`. You shouldn't need the boolean because it will stop changing the text once the counter finishes. – codeMagic Jun 09 '15 at 15:19
  • Yes, but this is just a workaround for this problem, which is, as I said, a simplified case of what I need. Doing so, will change the text in fixed times, while I want to change it due to user input – orizis Jun 09 '15 at 15:36
  • Your question is unclear. Why would you be using a `CountDownTimer` then? Maybe you want to [see here](http://stackoverflow.com/questions/16115708/disable-login-button-till-form-is-filled-android-sdk/16116118#16116118) about using a TextWatcher – codeMagic Jun 09 '15 at 15:39
  • I want to give the user tasks, which he should perform in a given time. I want to display the time remaining on the screen, along with the task to perform. When the user finishes the task, if there is still time on the clock, he will press a button and another task will appear. The goal is to finish as many tasks as possible in the given time. – orizis Jun 09 '15 at 15:44
  • That makes it more clear. When do you want to check the input? On every character, maybe the user clicks a button after attempting the task, something different? – codeMagic Jun 09 '15 at 15:48
  • The user will press a button to get a new task, whether he succeeded or just want to replace the task. For now, I just want the while loop to work properly :) – orizis Jun 09 '15 at 15:50
  • What I'm getting at is you don't need the loop. Once he presses the button, if the timer isn't finished, you give him a new task. From what I understand of what you want, you are making it more complicated than it needs to be – codeMagic Jun 09 '15 at 15:52
  • I think you are right, and I can make it work with different approach. I will try this, but I would still like to know why this happened. Anyway, thank you for all your help ! – orizis Jun 09 '15 at 16:07
  • To know *why exactly* it happened, I would have to ask more questions and it doesn't seem necessary since the approach is wrong. But, from what I understand, I would say it's going through the loop quickly so you are probably only seeing one value in that TextView. There is nothing to make it wait to update for the next iteration – codeMagic Jun 09 '15 at 16:47

1 Answers1

0

It's a little difficult to understand your games logic but the main problem i see with your code is that you are entering the loop in a lifecycle event handler. Take a look at this lifecycle description. You are stopping it in onCreate and there is still work to be done before the activity will finish its lifecycle handling.
I suggest you try making play round event bound or use a diffrent thread for it. there are alot of threading APIs for android and as i dont know the nature of your game rounds i cant recommend any.

Eytan
  • 728
  • 1
  • 7
  • 19