2012-06-22 129 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