Updating TextView using Timer and ArrayList

I am writing a small program to update TextView every 5 seconds. But I can’t update the text of the text every 5 seconds, especially when it comes to the last element of arraylist, for example: C below code

I follow this tutorial

public class MainActivity extends Activity {

 Timer timer;
 MyTimerTask myTimerTask;
 List<String> list;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  textCounter = (TextView)findViewById(R.id.counter);

  timer = new Timer();
  myTimerTask = new MyTimerTask();

  timer.schedule(myTimerTask, 1000, 5000);

  ....  

  list = new ArrayList<String>();
  list.add("A");
  list.add("B");
  list.add("C");

 }

 class MyTimerTask extends TimerTask {

  @Override
  public void run() {

   runOnUiThread(new Runnable(){

    @Override
    public void run() {
     for(int i=0; i<list.size(); i++) {
        textCounter.setText(list.get(i).toString());
     }
     }});
   }}
+4
source share
4 answers

Do not iterate over the list every time you call a method. Instead, get one item from the list and set it to a TextView.

private int count =0;
private class MyTimerTask extends TimerTask {
    @Override
    public void run() {
        runOnUiThread(new Runnable(){
            @Override
            public void run() {
                textCounter.setText(list.get(count%list.size).toString());
                count++;
            });
       }
    }
}
+2
source

Modify    MyTimerTask as shown below.

private int position = 0;

class MyTimerTask extends TimerTask {

    @Override
    public void run() {

        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                if (position >= list.size()) {
                    position = 0;
                }

                textCounter.setText(list.get(position).toString());
                position++;

            }
        });
    }


}
+1
source

TimerTask Handler TextView.

Handler mHandler = new Handler();
final Runnable runnable = new Runnable() {
     int count = 0;
     @Override
     public void run() {
         count++;
         textCounter.setText(list.get(count).toString());
         mHandler.postDelayed(this, 5000); // five second in ms
     }
};
mHandler.postDelayed(runnable, 1000); 

, .

+1

, Textview 5 .

Step 1: First, you simply create one saperate method, where Textview Update, as shown below.

 private void updateTextView(){
    /**
     * Write Your Textview Update Code
     */

}

Step 2 Declare an object of the Runnable class, which is the Just Call updateTextview method after a certain time.

 Runnable run = new Runnable() {

    @Override
    public void run() {

        updateTextView();

    }
};

Step 3 you can run this runnable using the following code.

YOUR_TEXTVIEW.postDelayed(run,5000);

I hope you understand my idea.

Best of luck

+1
source

Source: https://habr.com/ru/post/1624742/


All Articles