本文介绍了更新UI / runOnUiThread /最后的变量:如何写瘦code,做UI更新从另一个线程调用时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读上的UI更新时,我发现,喜欢的WinForms,机器人需要做的主线程UI更新(太糟糕了,我希望有人能一劳永逸解决这个讨厌的问题)

I have been reading up on the UI updating when I found out that, like WinForms, Android need to do UI updates on from the main thread (too bad, I was hoping someone could once and for all solve that irritating problem).

不管怎么说,我需要传递到UI线程。有人建议使用runOnUiThread方法,并且可能会奏效,如果不是为刺激性最终的话来发挥作用。

Anyways, I need to pass that to the UI thread. Some suggest the use of the runOnUiThread method and that might have worked, if it wasn't for that irritating "final" that then came into play.

我有一个接口,一个方法,我不能改变。它看起来是这样的:

I have an Interface, with a method I cannot change. It looks like this:

@Override
public void connStateChange(ClientHandler clientHandler)

当我得到的连接状态(网络)的变化该方法被调用。当我这样做,我需要做的一些东西,在这种情况下,它增加了一些文字,一个TextView。

That method is called when I get a change in the connection status (network). When I do, I need to do some stuff, and in this case it is adding some text to a TextView.

所以,我想这一点,但当然变量ClientHandler的不是最终的:

So I tried this, but of course the variable "clientHandler" isn't final:

@Override
public void connStateChange(ClientHandler clientHandler) {

   runOnUiThread(new Runnable()
   {
      public void run()
      {
        tv.append(clientHandler.getIP() + ", " + clientHandler.state.toString() + "\n");
        if (clientHandler.state == State.Connected)
        {
            tv.append("Loginserver hittad");
        }
      }
   });
}

我如何在一个不错的,干净,有效的方法写code在这个例子中更新UI?我需要访问我所有的变量等等...

How do I in a nice, clean and efficient way write code in this example to update the UI? I need access to all my variables etc...

推荐答案

试试看:

@Override 
public void connStateChange(ClientHandler clientHandler) {
    final ClientHandler temporaryHander = clientHandler;
    runOnUiThread(new Runnable() {
         public void run() {
               tv.append(temporaryHandler.getIP() + ", " + temporaryHandler.state.toString() + "\n"); 
               if (temporaryHandler.state == State.Connected) {
                    tv.append("Loginserver hittad");         
               }       
         }    
    }); 
} 

顺便说一句,code变得更具可读性,如果你声明没有anonim类正确的方法,但声明内部类出来的方法。把它作为图案的命令。

By the way, code becomes more readable, if you declare not anonim class right in the method, but declare inner class out of methods. Consider it as pattern Command.

更简洁和可重用code例。

Example of more clean and reusable code.

@Override 
public void connStateChange(ClientHandler clientHandler) {
    final ClientHandler temporaryHander = clientHandler;
    runOnUiThread(new MyRunnableCommand(temporaryHandler)); 
} 

private class MyRunnableCommand implements Runnable { 

     private ClientHandler clientHandler;

     public MyRunnableCommand(ClientHandler clientHandler) {
         this.clientHandler = clientHandler;
     }

     @Override
     public void run() {
               tv.append(clientHandler.getIP() + ", " + clientHandler.state.toString() + "\n"); 
               if (clientHandler.state == State.Connected) {
                    tv.append("Loginserver hittad");         
               }       
         } 

}

虽然Runnable的实现本身是夸大了一点,code变得更加可重用,易于阅读。

Though the Runnable implementation itself was inflated a little, code became more re-usable and easy to read.

这篇关于更新UI / runOnUiThread /最后的变量:如何写瘦code,做UI更新从另一个线程调用时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:25