0

基本的に私はカーディオ機能を行っており、3 つのカウントダウン タイマーが互いに入れ子になっているため、1 つのタイマーが終了すると次のタイマーが開始されます。1 つは準備時間、1 つはワークアウト時間、もう 1 つは休憩時間で、ユーザーはこれらの時間を選択します。

ユーザーがナンバーピッカーから選択する回数をループする必要がありますが、何をしても1回しか通過せず、ループしないため、すべてが機能することはわかっていますが、機能しないのはループ部分だけです。

ここで何か不足していますか?これを行うより良い方法はありますか?

    //Main countdown timers loop
    for(int i = 0; i <= times.getValue() + 1; i++) //times NumberPicker
    {
         prepCountTimer = new CountDownTimer(_finalPrep * 1000, 1000) {

             public void onTick(long millisUntilFinished) {

                 tvRoundCount.setText("Round " + roundCount + " / " + times.getValue());
                 tvCountDown.setText((millisUntilFinished / 1000) + "s");
                 if(millisUntilFinished <= (6 * 1000))
                 {
                     tvCountDown.setTextColor(Color.RED);
                 }
             }

             public void onFinish() {
                 workoutCountTimer = new CountDownTimer(_finalWorkout * 1000, 1000) {

                     public void onTick(long millisUntilFinished) {
                         tvCountDown.setTextColor(Color.GREEN);
                         tvCountDown.setText((millisUntilFinished / 1000) + "s");
                         if(millisUntilFinished <= 6 * 1000)
                         {
                             tvCountDown.setTextColor(Color.RED);
                         }
                     }

                     public void onFinish() {
                         restCountTimer = new CountDownTimer(_finalRest * 1000, 1000) {

                             public void onTick(long millisUntilFinished) {
                                 tvCountDown.setTextColor(Color.GREEN);
                                 tvCountDown.setText((millisUntilFinished / 1000) + "s");
                                 if(millisUntilFinished <= 6 * 1000)
                                 {
                                     tvCountDown.setTextColor(Color.RED);
                                 }
                             }

                             public void onFinish() {
                                 roundCount = roundCount + 1;
                             }
                          }.start();
                     }
                  }.start();
             }
          }.start();

    }
4

1 に答える 1

0

ここでの問題は、完了時に作成prepCountTimerして割り当ててから開始することです。次に、 for each の最後に到達し、再びループして別のものを作成preopCountTimerして開始します。restCountTimer完了したら、次の開始を行う必要がありますpreopCountTimer。ここで何か間違っていることを理解していない限り。

public void callingMethod() {
    timerMethod(times.getValue());
    // execution continues as your timer will run in a different thread
}

public void timerMethod(final int count) {
    if (count == 0) {
        // we have done the number of timers we want we can
        // call whatever we wanted to once our timers were done
    }
    //you could use count to get the times for each timer here
    startTimer(_finalPrep, new timerListner() {
        @Override
        public void timerFinish() {
            //when timer 1 finishes we will start timer 2
            startTimer(_finalWorkout, new timerListner() {
                @Override
                public void timerFinish() {
                    //when timer 2 finishes we will start timer 3
                    startTimer(_finalRest, new timerListner() {
                        @Override
                        public void timerFinish() {
                            //when timer 3 finishes we want to call the next timer in the list.
                            timerMethod(count - 1);
                        }
                    });
                }
            });
        }
    });
}

private interface timerListner {
    void timerFinish();
}

public void startTimer(int timerTime, final timerListner onFinish) {
    // you can pass in other parameters unqiue to each timer to this method aswell
    CountDownTimer timer = new CountDownTimer(timerTime * 1000, 1000) {
        public void onTick(long millisUntilFinished) {
            tvRoundCount.setText("Round " + roundCount + " / " + times.getValue());
            tvCountDown.setText((millisUntilFinished / 1000) + "s");
            if (millisUntilFinished <= (6 * 1000)) {
                tvCountDown.setTextColor(Color.RED);
            }
        }

        @Override
        public void onFinish() {
            onFinish.timerFinish();
        }
    };
    timer.start();

}
于 2013-02-15T20:45:56.893 に答える