2

I have a list view, which is showing multiple details with a edit text and count down timer text view.

For count down timer if i run a thread every 1000ms and subtract the time by 1000ms in the adapter data and do notify data set changed. It updates the timer data perfectly, but edit text state also gets updated which means as soon as user tried to write something focus gets cleared and value is also getting changed in the edit text.

Can anyone help with this problem ?

Thank you so much in advance.

Arun
  • 613
  • 1
  • 7
  • 14

1 Answers1

3

You can make use of the CountDownTimer class like this:

CountDownTimer cdt = new CountDownTimer(60000, 1000) {

    public void onTick(long millisUntilFinished) {
        TextView tv = (TextView) findViewById(R.id.tv);
        tv.setText("Time left: " + millisUntilFinished / 1000 + " seconds" );
    }

    public void onFinish() {
        TextView tv = (TextView) findViewById(R.id.tv);
        tv.setText("Time up!");
    }
}.start();

The above piece of code will start a countdown timer for 60 seconds and update the countdown text after every 1 second.

camelCaseCoder
  • 1,447
  • 19
  • 32
  • If we use countdowntimer for every item in list, on scrolling the list, the timer starts flickering the timer value. – Arun Feb 18 '16 at 05:13
  • Aren't you creating a new `CountDownTimer` object for every item in the `ListView`? – camelCaseCoder Feb 18 '16 at 06:06
  • Oh got it, whenever you scroll `getView()` gets called and creates a new `CountDownTimer` object and starts the countdown from start although the previous timer is still active and running. To avoid this, you'll have to manage the counter from your `Activity/Fragment` and call `notifyDataSetChanged()` rather than from your `Adapter`. Please have a look at this tutorial: http://blog.rochdev.com/2011/06/counters-in-listview.html – camelCaseCoder Feb 18 '16 at 06:17
  • notifyDataSetChanged updates the timer data perfectly, but edit text state also gets updated which means as soon as user tried to write something focus gets cleared and user is not able to type in the edit text. – Arun Feb 18 '16 at 06:57
  • Then try storing the `CountDownTimer` objects in a `HashMap` for each row and in `getView()` before creating a new object check whether an object has been initialized previously, if not only then create and start a new timer. – camelCaseCoder Feb 18 '16 at 07:03
  • CountDownTimer Runs on main thread. it keeps running after re loading the data from rest API and showing two different values one last and one newly fetched from server as flicker. One more important information, I am using recycler view. – Arun Feb 18 '16 at 10:46
  • Call `.cancel()` if it's already running and then start the timer again. – camelCaseCoder Feb 18 '16 at 10:48
  • How to check in Adapter if data is about to get updated ? I am doing the timer thing in Adapter. – Arun Feb 18 '16 at 11:36
  • 1
    Create a class `TimerStatus` with two members i.e. one `boolean` variable set to false by default and the other one a `CountDownTimer` object. Now in `getView()` check if `TimerStatus` object's boolean is false. If false, then initialize a timer, set the `boolean` to true and store that `TimerStatus` object in a `HashMap`. So next time for that row, obtain the `TimerStatus` obj and check if it's boolean is true (initialized) or false (uninitialized). To reset the timer, call `cancel()` if boolean is true and then start a new timer and store `TimerStatus` obj in `HashMap`. – camelCaseCoder Feb 18 '16 at 11:45