停止倒计时器的新人

时间:2014-01-30 06:58:59

标签: android listview timer countdowntimer

所以现在,我有一个列表视图设置了倒数计时器,但是如果我按下2个项目,那么它会开始显示2个项目的倒计时,如何在每次点击listview上的不同位置时设置它,它会停止旧的倒计时吗?谢谢你的帮助!

这是针对计时器的列表视图onclick

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();
            }

1 个答案:

答案 0 :(得分:0)

在开始新计时器之前尝试使用mCountDown.cancel()

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

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