简体   繁体   English

如何每x秒更新一个textview?

[英]how to update a textview every x second?

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.我正在尝试在文本视图中打印选择排序步骤,为此我想每 5 秒使用一次steptext.append()方法。

I have expected the result to look similar to this我希望结果看起来与此类似

排序步骤

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.但它不起作用,我得到了这个I/Choreographer: Skipped 1084 frames! The application may be doing too much work on its main thread. 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抱歉,我必须添加一些文字才能让我发布问题

Did you searched for the question?你搜索过这个问题吗?

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

How to pause / sleep thread or process in Android?如何在 Android 中暂停/睡眠线程或进程?

You shouldn't use Thread.sleep in android UI thread.您不应该在 android UI 线程中使用 Thread.sleep。 If there are some heavy work to run, do theme on another thread and when they done call ui thread and update textviews.如果有一些繁重的工作要运行,请在另一个线程上执行主题,完成后调用 ui 线程并更新文本视图。

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您可以使用另一种方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM