嗨,我的应用程序需要一个来自数据库的实时数据,我将它发布在我的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当我从变化值数据库先生。即使在日志中... –