我在Loaded ListView时需要一个计时器。就像“等待[(计时器)42]购买元素的秒数”我想向用户展示Textview喜欢它。我不能在ListView中使用Thread ...
我从runOnUiThread获取错误。为什么?我不能在Listview中使用计时器。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final TextView LblTime;
inflater=(LayoutInflater)Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.test,parent,false);
LblTime=(TextView)view.findViewById(R.id.LblFragmentAnaListViewKalanSure );
Timer T=new Timer();
T.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Context.getApplicationContext().runOnUiThread(new Runnable() {
@Override
public void run() {
long Time= MyTime.get(position).getTime() - (new Date()).getTime();
long Sec= Time/ 1000 % 60;
long Min= Time/ (60 * 1000) % 60;
LblTime.setText(Min+":"+Sec);
}
});
}
}, 1000, 1000);
return view;
}
答案 0 :(得分:1)
尝试使用CountDownTimer它在UI线程中完美运行。您所需要的只是在代码中启动它:
timer = new CountDownTimer(MS_TILL_COMPLETE, TICK_OFFSET) {
@Override
public void onTick(long l) {
//Repeating every TICK_OFFSET
}
@Override
public void onFinish() {
//Called after MS_TILL_COMPLETE
}
}.start();
答案 1 :(得分:1)
而不是计时器尝试使用CountDownTimer
new CountDownTimer(10000, 20000) {
@Override
public void onTick(long millisUntilFinished) {}
public void onFinish() {
}
}.start();
答案 2 :(得分:1)
试试这个..呼叫时间为42秒..
(new Handler()).postDelayed(new Runnable() {
public void run() {
// do you want
}
}, 42000);
如果你需要的每一秒都比这样做..
这是使用处理程序
初始化..
Handler handler = new Handler();
导入
import android.os.Handler;
将其称为..
handler.postDelayed(yourtask,42*1000);
private Runnable yourtask = new Runnable() {
public void run() {
// Run your code..
handler.removeCallbacks(yourtask);
handler.postDelayed(yourtask, 42*1000); // again start...
}
};
这是每秒使用一次计时器......
final Handler handler = new Handler();
Timer timer = new Timer();
private int DELAY = 1000 * 60 * 1;
称之为
timer.scheduleAtFixedRate(doAsynchronousTask, 0, DELAY);
在班级
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
// your code
}
});
}
};