Вопрос

I am fighting with my first app for Android: I have list of timers displayed (CountDowntTimer) in a List. When you tick(onItemClick) any of them the timer starts running. The timers work just fine, but when each timer ends I want to jump to the next element of the List and execute the next timer.

I don't know how to do this: Maybe I can call the onItemClick() method from the onFinish method of the CountDownTimer? I don't know how to do this with the correct arguments (AdapterView a, View v, int position, long id)

Is there any other way? Probably there is, but I don't know it.

Thanks in advance for your help!

Here you have some code:

lstRepeticiones.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                if(!timerHasStarted){

                String opcionSeleccionada = 
                            ((Repeticion)a.getAdapter().getItem(position)).getTitulo();
                lblEtiqueta.setText("Opción seleccionada: " + opcionSeleccionada);

                countDownTimer = new MyCountDownTimer((Integer.parseInt(datos[position].getDuracion().toString()) *1000), 1000,a , v , position , id);
                countDownTimer.start();
                timerHasStarted = true;
            }
            else{
                lblEtiqueta.setText("Elija opción");
                countDownTimer.cancel();
                timerHasStarted = false;
            }

There you can see how on each onItemClick I write a TextView (a message on the screen) according to some data. And after that I start the corresponding timer for that selection, and represent the CountDownTimer on the screen in the place where that option is. If there is already a timer running, everything will stop.

The idea now is that, when the timer ends, I want to go on with the next item on the list.

Это было полезно?

Решение

As you said, onItemClick is very convenient place to do this. But don't call onItemClick, just implement it in your activity class, to do that you need add this implements statement to your activity: ActivityName implements AdapterView.OnItemClickListener

public void onItemClick(AdapterView<?> adp, View view, int position, long id) {
    // Do whatever you want for the current view here
    // You want to start the timer here, you can also keep a global variable of this and call it currentlyRunningTimer.
    // at onFinish method, you can start another timer

   // The next view is at position + 1, listView is a variable for your ListView
   View nextItem = listView.getChildAt(position+1);

   if(nextItem != null){
      // Do whatever you want here, for the next item.
      // you can access items in the next item like this
      TextView text = (TextView) child.findViewById(R.id.text);
   }

}

Другие советы

For each item write code to start another timer in onFinish() method.

new CountDownTimer(30000, 1000) {

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

     public void onFinish() {
         //Start another CountDownTimer
     }
  }.start();

Good luck!

There's no need to call onItemClick() programatically, instead just call the code from onItemClick() directly. Anyway, it's not easy to give a good answer without seeing your code.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top