2012-06-22 68 views
-1

在写这篇文章之前,我看了其他文章,但还没有找到任何解决方案,我将不胜感激您的帮助。更新UI Android

在应用程序启动器类的onCreate方法中创建一个线程TCPServer并显示一个包含信息的表。 问题是,此信息会有所不同,并且必须在线程检测到TCPServer发送新消息控制时进行更新。

然后我显示代码,如果我表达自己。

//launcher class 
public class profesor extends Activity implements OnClickListener { 

    static TableLayout alumnsTable; 

    @Override 
    public void onCreate(Bundle savedInstanceState) {    
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     . 
     . 
     . 

     Thread serverThread = new Thread(null, new TCPServer(), "BackgroundService"); 
     serverThread.start(); 
     . 
     . 
     .  

     //information is added to the table   
     alumnsTable = (TableLayout)findViewById(R.id.alumnsTable); 
     List<Alumnos> listaAlumnos = Alumnos.ReadList(); 
     for(Alumnos al:listaAlumnos){ 
      alumnsTable.addView(filaAlumno(al)); 
     } 
    } 

    //Eliminates the above information and reloads the table with the new information 
    public void actualiza(){ 
     alumnsTable.removeAllViews(); 
     setContentView(R.layout.main); 
     alumnsTable = (TableLayout)findViewById(R.id.alumnsTable);       
     List<Alumnos> listaAlumnos = Alumnos.ReadList();                         
     for(Alumnos al:listaAlumnos){ 
      alumnsTable.addView(filaAlumno(al)); 
     } 
    } 
} 


//TCPServer class 
public class TCPServer implements Runnable { 
    private static Handler handler = new Handler(); 


    public void run() { 

    ServerSocket serverSocket; 

    String file; 
    int filesizeMb = 4; 
    int filesize = filesizeMb * (1024 * 1024); // filesize temporary hardcoded 


    int bytesRead; 
    int current = 0; 
    try { 
     serverSocket = new ServerSocket(profesor.COMUNICATION_PORT); 
     Log.d(profesor.LOG_TAG,"S: Servidor Iniciado."); 

     while (true) { 
      final Socket sock = serverSocket.accept(); 
      String ClientAddr=sock.getInetAddress().toString(); 
      ClientAddr = ClientAddr.substring(ClientAddr.indexOf('/') + 1, ClientAddr.length()); 
      final String contacto=DeviceList.getDeviceName(ClientAddr); 
      Log.d(profesor.LOG_TAG,"S: Conectado con: " + ClientAddr); 

      // Conection type 
      DataInputStream dis = new DataInputStream(sock.getInputStream()); 
      final String connectionType =dis.readUTF(); 
      Log.d(profesor.LOG_TAG,"S: Tipo de Conexion " + connectionType); 

      . 
      . 
      . 
      . 

      // RECEIVING CONTROL MESSAGE 
      if (connectionType.contains("CONTROL")) { 
       String ControlText =dis.readUTF(); 
       String[] Control = ControlText.split(":"); 
       Log.d(profesor.LOG_TAG,"S: Recibido nuevo progreso de prueba. IP: "+Alumnos.getIdAlumno(ClientAddr)+"->"+Integer.parseInt(Control[0])+" ->"+Integer.parseInt(Control[1])); 

       Alumnos.setProgress(Alumnos.getIdAlumno(ClientAddr), Integer.parseInt(Control[0]), Integer.parseInt(Control[1])); 


       /****************************************************/ 
       //Here is where I need to call the update method of the 
       //class teacher to delete the old data and fill the table 
       //again. 
       /****************************************************/ 
      } 
      dis.close(); 
      sock.close(); 
     } 

     } catch (IOException e) { 
      Log.d(profesor.LOG_TAG, "IOException"+e); 
      e.printStackTrace(); 
     } 
    } 
} 

任何人都可以给我一些想法?我见过的例子处理程序,但不是在类的声明,而在另一个呼叫。

我希望你明白我的意思,我的英语不是很好。

感谢您的帮助

+0

欢迎使用stackoverflow,请在发帖之前先搜索一下。这已经在许多场合讨论过了,例如http://stackoverflow.com/questions/4369537/update-ui-from-thread – Orlymee

回答

1

你可以尝试用一个处理器或活动引用创建TCPSERVER,然后使用它时,你想更新UI:

refHandler.post(new Runnable(){ 
    public void run() { 
     //Update 
    } 
}); 

refActivity.runOnUiThread(new Runnable() { 
    public void run() { 
     //Update 
    } 
}); 
+0

感谢您的快速响应,但我试图做这两种形式,并没有得到它的工作。 Eclipse在我的案例profesor.this中无法识别refActivity。如果在静态方法下更新,则Eclipse在类profesor的更新方法中给出“setContentView(R.layout.main)”失败。我不知道 – user1177917

+0

从我所看到的,问题是当我在线程TCP上时,上下文丢失。通过对班主任的介绍,我如何能够保护背景不会丢失? 谢谢 – user1177917

0

使用runOnUiThread从更新用户界面主题:

// RECEIVING CONTROL MESSAGE 
      if (connectionType.contains("CONTROL")) { 
       String ControlText =dis.readUTF(); 
       String[] Control = ControlText.split(":"); 
       Log.d(profesor.LOG_TAG,"S: Recibido nuevo progreso de prueba. IP: "+Alumnos.getIdAlumno(ClientAddr)+"->"+Integer.parseInt(Control[0])+" ->"+Integer.parseInt(Control[1])); 

       Alumnos.setProgress(Alumnos.getIdAlumno(ClientAddr), Integer.parseInt(Control[0]), Integer.parseInt(Control[1])); 


       /****************************************************/ 
       //Here is where I need to call the update method of the 
       //class teacher to delete the old data and fill the table 
       //again. 
       /****************************************************/ 
    profesor.this.runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      // refresh ui here 
     } 
    }); 
     } 
+0

要这样做,在profesor.this上有一个错误(范围内没有可以访问类型profesor的封闭实例)。 在调用profesor.actualiza()public void时遇到以下错误(无法对来自类型profesor的非静态方法actualizarPantalla()进行静态引用)。 问题是,如果框架作为静态方法方法更新,则会在类profesor的setContentView(R.layout.main)更新方法中给出相同的错误。 我要疯了 – user1177917

+1

从我所看到的,问题是当我在线程TCP上时,上下文丢失。通过对班主任的介绍,我如何能够保护背景不会丢失? 谢谢 – user1177917