计时器在列表视图中无法正常工作
Timer doesn't work correclty in listview
我有一个包含项目的 ListView 实例。每个项目都有一个用于秒表的计时器。
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
tempTime++;
Log.i(get_id() + "",tempTime + "");
}
}, 0,1000);
在activity
upTime.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
try{
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
UpdateTimes();
adapter.notifyDataSetChanged();
}
});
}catch(Exception e){
e.printStackTrace();
}
}
}, 0, 1000);
我给了period参数1秒。我在 activity 中创建了另一个计时器来更新 ListView,但在 LogOut 中打印时间早于 1 秒。我可以说它的周期大约是50毫秒
有什么问题?
如果您想在列表视图或项目中的任何地方显示时间,请在 android 中使用计时表,它将处理您的计时器,您可以通过启动和停止方法来管理它。
使用计时器显示时间:
<Chronometer
android:id="@+id/chronometer1"
android:textColor="#4169E1"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button5"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Chronometer" />
在你的 class 中:
Chronometer focus = (Chronometer) findViewById(R.id.chronometer1);
开始:
focus.start();
停止:
focus.stop();
重启:
focus.setBase(SystemClock.elapsedRealtime());
设置甲酸:
focus.setFormat("Formated Time - %s");
更多详情请关注下方link:
http://sampleprogramz.com/android/chronometer.php
http://developer.android.com/reference/android/widget/Chronometer.html
希望对你有帮助。
我有一个包含项目的 ListView 实例。每个项目都有一个用于秒表的计时器。
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
tempTime++;
Log.i(get_id() + "",tempTime + "");
}
}, 0,1000);
在activity
upTime.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
try{
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
UpdateTimes();
adapter.notifyDataSetChanged();
}
});
}catch(Exception e){
e.printStackTrace();
}
}
}, 0, 1000);
我给了period参数1秒。我在 activity 中创建了另一个计时器来更新 ListView,但在 LogOut 中打印时间早于 1 秒。我可以说它的周期大约是50毫秒
有什么问题?
如果您想在列表视图或项目中的任何地方显示时间,请在 android 中使用计时表,它将处理您的计时器,您可以通过启动和停止方法来管理它。
使用计时器显示时间:
<Chronometer
android:id="@+id/chronometer1"
android:textColor="#4169E1"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button5"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Chronometer" />
在你的 class 中:
Chronometer focus = (Chronometer) findViewById(R.id.chronometer1);
开始:
focus.start();
停止:
focus.stop();
重启:
focus.setBase(SystemClock.elapsedRealtime());
设置甲酸:
focus.setFormat("Formated Time - %s");
更多详情请关注下方link:
http://sampleprogramz.com/android/chronometer.php http://developer.android.com/reference/android/widget/Chronometer.html 希望对你有帮助。