我需要在TextView中显示Array中的许多String值。我正在使用Runnable。但它只运行一次!如果我放置一个for循环,代码会抛出错误。
Runnable hMyTimeTask = new Runnable() {
public void run() {
nCounter++;
label.setText(rates[nCounter]);
}
};
try {
handler.removeCallbacks(hMyTimeTask);
handler.postDelayed(hMyTimeTask, 1000); // delay 1 second
} catch (Exception e) {
e.printStackTrace();
}
使用for循环:
Runnable hMyTimeTask = new Runnable() {
public void run() {
for (int i = 0; i < rates.length; i++) {
label.setText(rates[i]);
}
}
};
try {
handler.removeCallbacks(hMyTimeTask);
handler.postDelayed(hMyTimeTask, 1000); // delay 1 second
} catch (Exception e) {
e.printStackTrace();
}
答案 0 :(得分:1)
我正在使用runnable。但它只运行一次!
在Runnable中,您需要再次发布Runnable以重复它:
Runnable hMyTimeTask = new Runnable() {
public void run() {
nCounter++;
if(nCounter >= rates.length)
nCounter = 0;
label.setText(rates[nCounter]);
// Run this again
handler.postDelayed(this, 1000); // delay 1 second
}
};