Android的 - CountDownTimer ListView中闪烁随机 [英] Android - CountDownTimer in ListView flickers randomly

查看:630
本文介绍了Android的 - CountDownTimer ListView中闪烁随机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用定时器列表视图,每一个不同的期限取决于数据库(类似于拍卖)

I am making listview with timers, each with different deadline depending on the database(similar to auction)

Time now = new Time();
now.setToNow();
now.normalize(true);
nowMillis = now.toMillis(true);
.
.

String endtime = a.get(position).get(TAG_ENDTIME);
Integer timeSecond = Integer.parseInt(endtime.substring(17, 19));
Integer timeMinute = Integer.parseInt(endtime.substring(14, 16));
Integer timeHour = Integer.parseInt(endtime.substring(11, 13));
Integer timeDay = Integer.parseInt(endtime.substring(0, 2));
Integer timeMonth = Integer.parseInt(endtime.substring(3, 5)) - 1;
Integer timeYear = Integer.parseInt(endtime.substring(6, 10));

Time future = new Time();
future.set(timeSecond, timeMinute, timeHour, timeDay, timeMonth, timeYear);
future.normalize(true);

long futureMillis = future.toMillis(true);
long interval = futureMillis - nowMillis;

new CountDownTimer(interval,1000)
{
    @Override
    public void onTick(long millisUntilFinished)
    {
        Long interval = millisUntilFinished;
        int days = (int) ((millisUntilFinished / 1000) / 86400);
        int hours = (int) (((millisUntilFinished / 1000) - (days * 86400)) / 3600);
        int minutes = (int) (((millisUntilFinished / 1000) - (days * 86400) - (hours * 3600)) / 60);
        int seconds = (int) ((millisUntilFinished / 1000) % 60);

        String countdown = String.format("%dd %dh %dm %ds", days, hours, minutes, seconds);
        holder.duration.setText(countdown);
    }

    @Override
    public void onFinish() 
    {
        // TODO Auto-generated method stub
        holder.duration.setText(TimeUp);
    }

}.start();

这code工作几乎完美的时候,只有一个实例。

That code works almost perfectly when there is only one instance.

然而在同一时间运行出现问题时,有几个实例,周围4-5计时器

However the problem arise when there is several instance, around 4-5 timer running at the same time

若干/所有的倒计时将开始闪烁,无论是秒,分钟,小时,或几天。

Several/all the countdown will start to flicker, be it seconds, minutes, hours, or days.

例如。我的27D 11H54米50年代和0D 23H47米0之间的定时闪烁之一

e.g. one of my timer flicker between 27d 11h 54m 50s and 0d 23h 47m 0s

由于这发生在无论在仿真器和设备上,这似乎是我的code的缺陷,但我没有一个线索,这是什么原因。

Since this occur on both on emulator and on my device, it seems to be my code's flaw, but I don't have a clue what could cause this.

我试图改变

holder.duration.setText(倒计时) holder.duration.setText(millisUntilFinished)

和所需的时间和巨大的,随机数的倒计时闪烁,

and the the countdown flickers between the desired duration and a huge, random number,

请帮忙。

推荐答案

您应该使用一个的TimerTask 键,把你所有的UI更新成一个定时器,而不是运行多个 CountDownTimer S表示基本相同的工作,因为你已经做所有的数学运算,以确定何时时间到了对任何特定的项目,你可能只是运行一个的TimerTask ,一旦第二有它更新了一切。 CountDownTimer 是一个有用的实施倒计时,因为它做一些内置的数学,等你重做所有的数学,所以你还不如用一个实例一个普通的的TimerTask

You should use one TimerTask and put all your UI updates into that single timer instead running multiple CountDownTimers for essentially the same job since you're already doing all the math to determine when "time is up" for any particular item, you might just run one TimerTask and once a second have it update everything. CountDownTimer is useful for a single implementation count down because it does some built-in math, etc. You're redoing all that math, so you might as well use one instance of a regular TimerTask.

CountDownTimer 继电器在处理器的消息预定延迟执行。单倒计时实例是不可能导致任何怪异的行为,但如果你有几个会,所有应该嘀的时候,系统时钟击中了每个秒(在米利斯的时间,000结尾 - 每秒一次,并且所有在同一时间),那么这些处理器都将尝试同时开火,并注定要失败。

The implementation of CountDownTimer relays on scheduled delays in Handler messaging. A single countdown instance is unlikely to result in any bizarre behavior, but if you have several going that all supposed to "tick" when the system clock hits each second (the time in millis ends in "000" - once a second, and all at the same time), then those handlers will all try to fire simultaneously and inevitably fail.

如果用户界面或其他过程可能会耽误一些消息,甚至到了点,它就会跳过蜱,迎头赶上。此外,这意味着下一个消息延迟可能是从下一个周期(即只毫秒,如果它应该检查每1000米利斯,但被延迟一个额外的1990年米利斯,那么它会跳过一个勾,还可以调度的下一条消息10米利斯未来。

If the UI or other process will likely delay some of these messages, even to the point where it will "skip ticks" to catch up. Also, that means that the next message delay could be only milliseconds from the next tick (i.e. if it's supposed to check every 1000 millis, but is delayed an additional 1990 millis, then it will skip a tick and also schedule the next message for 10 millis into the future.

这篇关于Android的 - CountDownTimer ListView中闪烁随机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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