0

I'm trying to print the selection sort steps in a text view, in order to do that I want to use each steptext.append() method after every 5 seconds.

I have expected the result to look similar to this

sortSteps

but it's not working, and I'm getting this I/Choreographer: Skipped 1084 frames! The application may be doing too much work on its main thread.

    public class steps extends Fragment {
    private static final String ARG_PARAM1 = "inpuut";
    private String minpuut;
    private OnFragmentInteractionListener mListener;

    private TextView inputEntred  ;
    private TextView  steptext ;

    @Override
    public void onResume() {
        super.onResume();
        String[] numberList = inputEntred.getText().toString().split(","); 

        final Integer[] numbers = new Integer[numberList.length];

        for (int i = 0; i < numberList.length; i++) {

            numbers[i] = Integer.parseInt(numberList[i]);
        }
    }

    public static int  Nswaps =0; 

     public String selectionSteps(Integer[] arr) throws InterruptedException {
        System.out.println(Arrays.toString(arr));
        int n = arr.length;
        int Ccounter=0; 

        for (int i = 0; i < n-1; i++)
        {          
            int min_idx = i;    
            for (int j = i+1; j < n; j++)   {
                Ccounter++;                  
             steptext.append(" comparison "+Ccounter +" :  comparing "+ arr[i]+" and "+ arr[j]+" \n");
                Thread.sleep(5000);

                if (arr[j] < arr[min_idx])
                {
                    min_idx = j ;
                    steptext.append( "("+arr[j]+"<"+arr[i] +") TRUE ,  swap positions \n");
                    Thread.sleep(5000);
                    swap(arr,min_idx, i);
                    steptext.append("  swap  : ");
                    steptext.append(Arrays.toString(arr)+"\n");
                    Thread.sleep(5000);
                    }
                else

                steptext.append( "("+arr[j]+"<"+ arr[i]+")   FALSE , no swaping \n");;
                Thread.sleep(5000);

            }
            swap(arr,min_idx, i);
        }

        System.out.println(Ccounter);


        return null;
    }

    public static void swap(Integer[] a, int i, int j) {
        int e = a[i];
        a[i] = a[j];
        a[j] = e;
        Nswaps++ ;
    }
}

sorry I have to add some text to let me post the question

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
aphro19
  • 17
  • 4
  • 1
    Looks like you're calling `Thread.sleep()` from the main thread, which would explain the warning/error message you're getting. You should offload the work to another thread, then only update your TextView from the main thread. – Husayn Hakeem Mar 13 '20 at 22:15

1 Answers1

1

Did you searched for the question?

https://stackoverflow.com/a/40104112/6026739

How to pause / sleep thread or process in Android?

You shouldn't use Thread.sleep in android UI thread. If there are some heavy work to run, do theme on another thread and when they done call ui thread and update textviews.

In your case you can use for something like this :

Handler handler = new Handler();
updater = new Runnable() {
    @Override
    public void run() {
        // Do one step of sorting here
        steptext.append("Your swaps steps");
        handler.postDelayed(updater,5000);
            }
    };
handler.post(updater);

You can use another method

Erfan
  • 83
  • 4
  • 9