Reputation:
Good day guys, please am building a soccer prediction app, and I want to show countdown to the match time. Once the game starts I call another function, but I cant get the countdown right.
Here is what have tried:
public void onSuccess(String response) {
try {
JSONObject json = new JSONObject(response);
userid.setText("Welcome: "+ usermail);
hometeam.setText(json.getString("Home_Team"));
awayteam.setText(json.getString("Away_Team"));
home_logo.setImageUrl(url+json.getString("Home_Logo"));
away_logo.setImageUrl(url+json.getString("Away_Logo"));
game_id.setText(json.getString("ID"));
match_date.setText(json.getString("Date")+" "+json.getString("Time"));
//month.setText(json.getInt(name))
SimpleDateFormat mdate = new SimpleDateFormat("yyyy-MM-dd");
Date m_date= (Date) match_date.getText();
CountDownTimer cdt = new CountDownTimer(m_date.getTime(), 1000) {
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
Log.d("tick","clocks ticks");
}
public void onFinish() {
// TODO Auto-generated method stub
}
}.start();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
But once I run the app, it crashes. It was working fine until I added the countdown function. date and time comes back in this format. "2013-12-23 20:00:00"
. Thanks.
Upvotes: 0
Views: 6653
Reputation: 1230
Without a Logcat I cannot tell if there are multiple things wrong, or what exactly is happening.
getTime();
returns a Long
, the number of milliseconds since last Unix Epoch (1st Jan 1970). This will be a large number like 1387233645 or such.
See here for Wikipedia article on Unix Time
The constructor for the CountdownTimer is in the form
CountDownTimer(long millisInFuture, long countDownInterval)
getTime() is returning the milliseconds since 01/01/1970 not millis in the future.
For an example, try a 30 second timer with default values: Also from Docs
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
Upvotes: 1