Display values in ArrayList one by one in textView

Jimit

I am trying to display values inside ArrayList on single line textView one by one after some interval. How to achieve this without blocking the main thread?

I have written code which is able to do this with Thread.sleep but, after a few seconds of running, activity is getting crashed. I have used For Loop & Thread.sleep to iterate every ArrayList value after some interval.

When activity crashes, I am getting IndexOutOfBondException after a few seconds of running.

public void errorRepeater() {

    Thread t = new Thread() {

        @Override
        public void run() {
            //  !isInterrupted()

            while (!isInterrupted()) {
                for (xz = 0; xz < errorList.size(); xz++) {
                    try {
                        Thread.sleep(2000);  //1000ms = 1 sec

                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                String sErrorList = errorList.get(xz);
                                String sErrorListOkBox = errorListOkBox.get(xz);
                                Log.i("MQTT sErrorList", sErrorList);
                                TextView tvC1HPLP = findViewById(R.id.errormsg);
                                tvC1HPLP.setText(sErrorList);
                                TextView tvok = findViewById(R.id.ok);
                                tvok.setText(sErrorListOkBox);
                                rl.setBackgroundResource(R.drawable.errorred);
                                tvC1HPLP.setTextColor(Color.RED);

                            }
                        });

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

    t.start();

}

textView should display values inside ArrayList one by one without crashing activity.

DHAVAL ASODARIYA

Just for reference, you can try something like this.

   // You can define those both textview globally.
   TextView tvC1HPLP = findViewById(R.id.errormsg);
   TextView tvok = findViewById(R.id.ok);

   Handler mHandler = new Handler();
   final Runnable runnable = new Runnable() {
     int count = 0;
     @Override
     public void run() {

         String sErrorList = errorList.get(count%errorList.size);
         String sErrorListOkBox = errorListOkBox.get(count%errorListOkBox.size);

         tvC1HPLP.setText(sErrorList);

         tvok.setText(sErrorListOkBox);
         rl.setBackgroundResource(R.drawable.errorred);
         tvC1HPLP.setTextColor(Color.RED);
         count++;
         mHandler.postDelayed(this, 4000); // four second in ms
     }
   };
   mHandler.postDelayed(runnable, 1000);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Display one TextView in bottom android

When adding Values to Arraylist from one class to another, Listview does not display updated values

Is there any way to make a ArrayList of TextView from one TextView defined in xml?

How to display TextView and EditText in one line in Android?

display array values in one jlabel

Show values of same key in one TextView in Recyclerview

How to display ArrayList in TextView?

Check if values in one arraylist is present in another

ArrayList. Joining String values in one index

How to display value of two Spinner(present in one activity) in a TextView: Android

How to set a TextView to display text in one line in Android

How to pass int value from one activity to another and display in TextView?

Display the values from different column in one column

JSON - display values and adding new one line

Display two different values by selecting one dropdown

How to display ArrayList value in TextView?

Display multiple values in one column based on values in multiple columns in Excel

Three textView in one row

In ArrayList, group by a key and perform summation over one of the values

Sorting values of an arraylist objects and printing multiple of them in one lambda

ArrayList of ArrayLists is overridden when setting values of a second one

android retrieve one or few values from arraylist from database

Adding one arraylist to another

over reliance on one arrayList

Initialization of an ArrayList in one line

Merge 3 arraylist to one

Display list of data - one by one

Import and display image one by one

Display blocks one by one setTimeout