Android Countdown计时器-使用Array Value循环遍历ArrayList重新启动计时器 [英] Android Countdown Timer - Loop through ArrayList restarting timer with List Value

查看:61
本文介绍了Android Countdown计时器-使用Array Value循环遍历ArrayList重新启动计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个整数ArrayList'timerList'(例如-10、20、10),我想将其用于倒数计时器.

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

我希望遍历列表,并在计时器结束后重新启动计时器,以便它使用列表中的下一个值,因此它从10开始倒数​​,然后重置为20,然后重置为10,直到所有值都在该列表已被使用.

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.

我苦苦挣扎的部分是遍历ArrayList的值.我可以从ArrayList设置初始值,然后尝试使用onFinish()中的数组列表进行下一次设置.

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.

我尝试创建一个int变量来跟踪我在列表中的位置,然后每次都向其添加1,以获取下一个列表值,但是一旦它计数一次,它就停留在0

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!

先谢谢了保罗

推荐答案

我通过创建倒数计时器数组解决了这个问题

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;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text= (TextView) findViewById(R.id.text);
        timeList=new ArrayList<>();
        timeList.add(10*1000);
        timeList.add(20*1000);
        timeList.add(30*1000);
        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) {
                @Override
                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)));
                    text.setText(texts);
                }

                @Override
                public void onFinish() {
                     if(!((finalI +1)>=timeList.size())){
                         countDownTimers[finalI+1].start();
                     }
                }
            };
        }
        countDownTimers[0].start();

    }

}

这篇关于Android Countdown计时器-使用Array Value循环遍历ArrayList重新启动计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆