How do I pause and resume a cycle?

I am an Android nob and I am trying to cut text from an array of a list of strings and display them in the textswitcher. I want the text to change every two seconds. i used this SO question as my explorer and rearranging the text with a button without any problem. However, when I try to loop through the text with a for loop with a 2 second delay, it only displays the first text from arrayList. How do I make the loop run continuously with a pause? Any help is appreciated.

My code;

private void updateCounter()
{   

    try{
    for (int i=0; i< CoinShowReader.tickercontent.size(); i++){                             

        mHandler.postDelayed(new Runnable() { 
             public void run() { 
                m_switcher.setText((CoinShowReader.tickercontent.get(CoinShowReader.m_counter)));               
                CoinShowReader.m_counter++;
             } 
        }, 2000);

        }

    }catch(Exception e){
        e.printStackTrace();    
    }
}  

      

+3


source to share


1 answer


remove the loop, you don't need that, just schedule the dust to be run inside the handler like this:

void updateTextView(){
        m_switcher.setText((CoinShowReader.tickercontent.get(CoinShowReader.m_counter)));               
        CoinShowReader.m_counter++;

            mHandler.postDelayed(new Runnable() { 
                             public void run() { 
                               updateTextView(); 
        } } ,2000);  }
    }

      



so each call updateTextView()

assigns the next call, and so on.

Note. remember to insert a trigger to stop this behavior as it is infinite

+4


source







All Articles