嗨,我的應用程序需要一個來自數據庫的實時數據,我將它發佈在我的TextView
上,並且我無法在數據庫更新時更新TextView
。我嘗試使用Timer
,但它仍然是一樣的。Android Studio每5秒更新一次textview
這裏是我的代碼,
public void startTimer() {
//set a new Timer
timer = new Timer();
//initialize the TimerTask's job
initializeTimerTask();
timer.schedule(timerTask, 0, 5000);
}
private void stopTimerTask() {
//stop the timer, if it's not already null
if (timer != null) {
timer.cancel();
timer = null;
}
}
public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
final AcceptCars Cars = (AcceptCars) getIntent().getSerializableExtra("cars");
renterLat.setText(Cars.renterLat);
renterLng.setText(Cars.renterLng);
Log.d(TAG,renterLat.getText().toString());
Log.d(TAG,renterLng.getText().toString());
}
});
}
};
}
而且這裏是我得到的Cars.renterLat
和Cars.renterLng
,
public class AcceptCars implements Serializable {
@SerializedName("renterLat")
public String renterLat;
@SerializedName("renterLng")
public String renterLng;
}
更新的文字應該是裏面** ** runOnUiThread是 – Saveen
請不要讓我知道,如果你還有什麼問題 – Saveen
@Saveen - 它仍然沒有更新的TextView當我從變化值數據庫先生。即使在日誌中... –