birukhimself
birukhimself

Reputation: 193

Running a countdown timer in a for loop causing the timing not to work

I've been trying to run a countdown timer in a for loop but whenever I do, instead of the countdown timer keeping its timing and carrying out some code in the onTick() method, all the code in the onTick() method gets carried out instantly without the delay. Any way to fix this, or an alternative approach to what i'm trying to achieve? Here's my code :

    public void runFromString(String theString, final TextView textView) {
    for (final Lyric lyric : lyricArrayList(theString)) {
        final int[] j = {0};
        new CountDownTimer(lyric.timeInMillis(), 1000) {
            @Override
            public void onTick(long l) {
                if (j[0] == 0) {
                    textView.setText(lyric.getLyricText());
                    j[0] = 1;
                }
            }

            @Override
            public void onFinish() {
                textView.setText("");
                j[0] = 0;
            }
        }.start();
    }
}

Upvotes: 0

Views: 84

Answers (1)

IldiX
IldiX

Reputation: 601

I am not 100% clear what you are trying to achieve but your onTick() method is supposed to run every second until it reaches lyric.timeinMillis(). Based on your condition it runs only the first time (since you change the first cell of the vector to 1).

Upvotes: 1

Related Questions