Question

So right now, I have a listview that set up a countdowntimer, but if I press 2 items, then it starts showing the countdown of 2 items, how can I set it up that everytime a different position on listview is clicked, it stops the old countdown for the new one? Thanks for all the help!

Here is the listview onclick for the timers

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parentAdapter, View view, int position,long id) {

                final TextView mTextField = (TextView) findViewById(R.id.timerValue);

                if(position == 0) {      

                    mCountDown = new CountDownTimer((300 * 1000), 1000) {

                         public void onTick(long millisUntilFinished) {
                             mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
                         }

                         public void onFinish() {
                             mTextField.setText("Session Completed!");
                             String path = "/sdcard/Music/ZenPing.mp3";
                            try {

                                mp.reset();
                                mp.setDataSource(path);
                                mp.prepare();
                                mp.start();

                            } catch (IOException e) {
                                Log.v(getString(R.string.app_name), e.getMessage());
                            }
                         }
                      }.start();

                } else if (position == 1) {
                    mCountDown = new CountDownTimer((600 * 1000), 1000) {

                         public void onTick(long millisUntilFinished) {
                             mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
                         }

                         public void onFinish() {
                             mTextField.setText("Session Completed!");
                             String path = "/sdcard/Music/ZenPing.mp3";
                                try {

                                    mp.reset();
                                    mp.setDataSource(path);
                                    mp.prepare();
                                    mp.start();

                                } catch (IOException e) {
                                    Log.v(getString(R.string.app_name), e.getMessage());
                                }
                         }
                      }.start();
                } else if (position == 2) {
                    mCountDown = new CountDownTimer((900 * 1000), 1000) {

                         public void onTick(long millisUntilFinished) {
                             mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
                         }

                         public void onFinish() {
                             mTextField.setText("Session Completed!");


     String path = "/sdcard/Music/ZenPing.mp3";
                            try {

                                mp.reset();
                                mp.setDataSource(path);
                                mp.prepare();
                                mp.start();

                            } catch (IOException e) {
                                Log.v(getString(R.string.app_name), e.getMessage());
                            }
                     }
                  }.start();
            }
Was it helpful?

Solution

Try to use mCountDown.cancel() before starting the new timer:

if(position == 0) {      
                    mCoundDown.cancel();
                    mCountDown = new CountDownTimer((300 * 1000), 1000) {

                         public void onTick(long millisUntilFinished) {
                             mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
                         }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top