Note that there are some explanatory texts on larger screens.

plurals
  1. POPrevious state of timer is not cleared
    primarykey
    data
    text
    <p>I am trying to update a text view with time elapsed since a button was pressed, following is my code:</p> <pre><code> public void startTimeCounter() { tripTimeCounter = new CountDownTimer(60 * 1000, 1000) { @Override public void onFinish() { // TODO Auto-generated method stub repeatCounter = repeatCounter + 1; startTimeCounter(); } @Override public void onTick(long millisUntilFinished) { // TODO Auto-generated method stub timedisplay = (TextView) findViewById(R.id.textView3); timedisplay.setText(formatInterval((repeatCounter * 60) * 1000 - millisUntilFinished)); } }.start(); } private static String formatInterval(final long l) { final long hr = TimeUnit.MILLISECONDS.toHours(l); final long min = TimeUnit.MILLISECONDS.toMinutes(l - TimeUnit.HOURS.toMillis(hr)); final long sec = TimeUnit.MILLISECONDS.toSeconds(l - TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min)); final long ms = TimeUnit.MILLISECONDS.toMillis(l - TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min) - TimeUnit.SECONDS.toMillis(sec)); return String.format("%02d:%02d:%02d", hr, min, sec, 0); } </code></pre> <p>The timer works perfectly, however when I press the button for the second time the value in the text view is not cleared, but instead a new time is associated which runs in parallel with the previous timer, I can see the previous time and a reflection of the new time incrementing simultaneously . I want to know as to how will I clear the previous state, before a second call is made to startTimeCounter();</p> <p>The button Click Code: </p> <pre><code> private View.OnClickListener but = new View.OnClickListener() { @Override public void onClick(View v) { if (isOnline() == true) { // PHP(); // network(); startTimeCounter(); } else { Toast.makeText(getApplicationContext(), "Not connected to the internet", Toast.LENGTH_LONG) .show(); } } }; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload