Question

I'm a noob to Android and I am trying to cycle text from an arrayList of strings and display them in an textswitcher. I want the text to change every two seconds. i used this SO question as my guide and have no problem switching the text with a button. However, when I try to cycle the text with a for loop with a 2 second delay it only shows the first text from the arrayList. How can I make the loop continuously execute with a pause? Any help is greatly 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();    
    }
}  
Was it helpful?

Solution

remove the loop, you don't need it, just schedule anther runnable 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);  }
    }

that way every call to updateTextView() schedule the next call and so on ...

Note: don't forget to insert trigger to stop that behavior because it's infinity

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top