Search code examples
androidtextviewcountdowntimerdo-loops

Can't get coundown timer to work or display in my program


I'm a newer programmer and this is my first project but I'm having a bit of trouble in making a proper loop with three timers that are supposed to run one after the other. I managed to get the objects to hold the values they are supposed to within the loop but for some reason, the timer isn't displaying in the text field like it should.

          startBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("mTimer:", String.valueOf(mTimer));
            Log.i("mReps:", String.valueOf(mReps));
            Log.i("Flexion:", String.valueOf(flexionTimer));
            Log.i("Hold:", String.valueOf(holdTimer));
            Log.i("Extension:", String.valueOf(extensionTimer));

            for (int iter = 0; iter < mReps; iter++) {
                Log.i("Loop:", String.valueOf(iter));

                final Timer workingFlexionTimer = new Timer();
                workingFlexionTimer.schedule(new TimerTask() {
                    int counter = ((int) flexionTimer / 1000);
                    @Override
                    public void run () {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mPhase.setText("Flexion");
                                mCountDownTimer.setText("" + String.format(String.valueOf(counter + 1)));
                            }
                        });
                        if (counter-- == 0) {
                            workingFlexionTimer.cancel();
                        }
                    }
                }, 0, 1000);


                final Timer workingHoldTimer = new Timer();
                workingHoldTimer.schedule(new TimerTask() {
                    int counter = ((int) holdTimer / 1000);
                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mPhase.setText("Hold!!!");
                                mCountDownTimer.setText("" + String.format(String.valueOf(counter + 1)));
                            }
                        });
                        if (counter-- == 0) {
                            workingHoldTimer.cancel();
                        }
                    }
                }, flexionTimer, 1000);

                final Timer workingExtensionTimer = new Timer();
                workingExtensionTimer.schedule(new TimerTask() {
                    int counter = ((int) extensionTimer / 1000);
                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mPhase.setText("Extension");
                                mCountDownTimer.setText("" + String.format(String.valueOf(counter + 1)));
                            }
                        });
                        if (counter-- == 0) {
                            workingExtensionTimer.cancel();
                        }
                    }
                }, (flexionTimer + holdTimer), 1000);
            }

I'm kind of at a loss at this point and any suggestion would be appreciated.


Solution

  • UPDATE

    For timers I always do:

        final TextView textView = (TextView)findViewById(R.id.textView);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int counter = 10;
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText(""+counter);
                    }
                });
    
                if (counter-- == 0){
                    timer.cancel();
                }
    
            }
        }, 0, 1000);
    
    timer.schedule(new TimerTask() {
            int counter = 10;
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText(""+counter);
                    }
                });
    
                if (counter-- == 0){
                    timer.cancel();
                }
    
            }
        }, 10000, 1000);
    
    
    timer.schedule(new TimerTask() {
            int counter = 10;
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText(""+counter);
                    }
                });
    
                if (counter-- == 0){
                    timer.cancel();
                }
    
            }
        }, 20000, 1000);
    

    For more info check this

    In the link...

    public void schedule(TimerTask task, long delay, long period)

    The above code. Start the run() without delay.

    long delay = 0;// in ms
    long period = 1000;// in ms
    

    So every 1000ms call the run() and counter--. When counter = 0 cancel.

    If you want to run, the one after the other, put delay.

    UPDATE

    Now the first will run immediately, the second will wait 10000ms (10s) and will run, finaly the third will wait 20000ms (20s) and then run.