Android - howto pass data to the Runnable in runOnUiThread?

前端 未结 6 2339
慢半拍i
慢半拍i 2020-12-08 10:30

I need to update some UI and do it inside of the UI thread by using runOnUiThread
Now the data for the UI comes from the other Thread, represented by

6条回答
  •  無奈伤痛
    2020-12-08 10:56

    I had a similar problem where I wanted to pass information into the thread. To solve it with the android system, I modifying corsiKa's answer in: Runnable with a parameter?

    You can declare a class right in the method and pass the param as shown below:

    void Foo(String str) {
        class OneShotTask implements Runnable {
            String str;
            OneShotTask(String s) { str = s; }
            public void run() {
                someFunc(str);
            }
        }
        runOnUiThread(new OneShotTask(str));
    }
    

提交回复
热议问题