带有多个时钟的Listview

时间:2014-06-19 22:15:12

标签: java android listview timer runnable

我正在尝试创建一个包含多个时钟的列表视图,但每行中的时间总是跳来跳去。

在我的getView中我有

        Date date=new Date();
        chagre.setTag(position);
        Handler mClockHandler = new Handler();
        mUpdateClockTask clockTask = new mUpdateClockTask(chagre,
                chagre.getTag().toString(), date);
        mClockHandler.post(clockTask);

并且可以运行

public class mUpdateClockTask implements Runnable {
    private TextView tv;
    Handler mClockHandler = new Handler();
    String tag;
    Date date;

    public mUpdateClockTask(TextView tv,
            String tag, Date date) {
        this.tv = tv;
        this.tag = tag;
        this.date = date;
    }

    public void run() {
        if (tv.getTag().toString().equals(tag)) {
            SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("h:mm:ss a");
            String time = TIME_FORMAT.format(date);                 
            tv.setText(time.trim());
            mClockHandler.postDelayed(this, 1000);
            notes.notifyDataSetChanged();
        }
    }

};

1 个答案:

答案 0 :(得分:0)

Android不会同时在列表中创建所有行 - 它会在您滚动到它们并在之后销毁它们时创建它们,或者通常会回收列表中其他位置出现的行。您的Runnables引用了TextView,该{{1}}用于列表中的随机行,可能会被意外销毁。

相关问题