user1065871 user1065871 - 6 months ago 82
Android Question

Android Countdown Timer - Loop through ArrayList restarting timer with List Value

I've got an Integer ArrayList 'timerList' (eg - 10, 20, 10) of values which I then want to use for a Countdown Timer.

I'm looking to loop through the list and restart the timer once it finishes so that it uses the next value from the list, so it counts down from 10 and then resets to 20 and then resets to 10 until all values in the list have been used.

The part i'm struggling with is looping through the values of the ArrayList. I can set the initial value from the ArrayList and I'm then trying to use the array list within the onFinish() to set the next time.

I've tried creating an int variable to keep track of where abouts I am in the list and then adding 1 to this every time, to get the next list value but once it has counted down once, it just sticks at 0

Any advice or examples of how I can implement this correctly (perhaps even looking at it from a different perspective if I'm not heading in the right direction) would be greatly appreciated!

Thanks in advance


I solved this problem by creating array of countdown timers

public class MainActivity extends AppCompatActivity {
    CountDownTimer[] countDownTimers;
    int Time;
    TextView text;
    ArrayList<Integer> timeList;
    int i=0;
    protected void onCreate(Bundle savedInstanceState) {
        text= (TextView) findViewById(;
        timeList=new ArrayList<>();
        countDownTimers=new CountDownTimer[timeList.size()];
        for(int i=0;i<timeList.size();i++){
            final int finalI = i;
            countDownTimers[i]=new CountDownTimer(timeList.get(finalI),100) {
                public void onTick(long millisUntilFinished) {
                    long ms = millisUntilFinished;
                    String texts = String.format("%02d : %02d",
                            TimeUnit.MILLISECONDS.toMinutes(ms) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(ms)),
                            TimeUnit.MILLISECONDS.toSeconds(ms) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(ms)));

                public void onFinish() {
                     if(!((finalI +1)>=timeList.size())){