Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 17th, 2012  |  syntax: None  |  size: 4.60 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Update UI Android
  2. //launcher class
  3. public class profesor extends Activity implements OnClickListener {
  4.  
  5.     static TableLayout alumnsTable;
  6.  
  7.     @Override
  8.     public void onCreate(Bundle savedInstanceState) {              
  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.main);
  11.  
  12.         .
  13.         .
  14.         .
  15.  
  16.         Thread serverThread = new Thread(null, new TCPServer(), "BackgroundService");
  17.         serverThread.start();
  18.         .
  19.         .
  20.         .      
  21.  
  22.         //information is added to the table        
  23.         alumnsTable = (TableLayout)findViewById(R.id.alumnsTable);
  24.         List<Alumnos> listaAlumnos = Alumnos.ReadList();
  25.         for(Alumnos al:listaAlumnos){
  26.             alumnsTable.addView(filaAlumno(al));
  27.         }  
  28.     }
  29.  
  30.     //Eliminates the above information and reloads the table with the new information
  31.     public void actualiza(){
  32.         alumnsTable.removeAllViews();
  33.         setContentView(R.layout.main);
  34.         alumnsTable = (TableLayout)findViewById(R.id.alumnsTable);                          
  35.          List<Alumnos> listaAlumnos = Alumnos.ReadList();                                                                                                  
  36.         for(Alumnos al:listaAlumnos){
  37.             alumnsTable.addView(filaAlumno(al));
  38.         }
  39.     }
  40. }
  41.  
  42.  
  43. //TCPServer class
  44. public class TCPServer implements Runnable {
  45.     private static Handler handler = new Handler();
  46.  
  47.  
  48.     public void run() {
  49.  
  50.     ServerSocket serverSocket;
  51.  
  52.     String file;
  53.     int filesizeMb = 4;
  54.     int filesize = filesizeMb * (1024 * 1024); // filesize temporary hardcoded
  55.  
  56.  
  57.     int bytesRead;
  58.     int current = 0;
  59.     try {
  60.         serverSocket = new ServerSocket(profesor.COMUNICATION_PORT);
  61.         Log.d(profesor.LOG_TAG,"S: Servidor Iniciado.");
  62.  
  63.         while (true) {
  64.             final Socket sock = serverSocket.accept();
  65.             String ClientAddr=sock.getInetAddress().toString();
  66.             ClientAddr = ClientAddr.substring(ClientAddr.indexOf('/') + 1, ClientAddr.length());
  67.             final String contacto=DeviceList.getDeviceName(ClientAddr);
  68.             Log.d(profesor.LOG_TAG,"S: Conectado con: " + ClientAddr);
  69.  
  70.             // Conection type
  71.             DataInputStream dis = new DataInputStream(sock.getInputStream());
  72.             final String connectionType =dis.readUTF();
  73.             Log.d(profesor.LOG_TAG,"S: Tipo de Conexion " + connectionType);
  74.  
  75.             .
  76.             .
  77.             .
  78.             .
  79.  
  80.             // RECEIVING CONTROL MESSAGE
  81.             if (connectionType.contains("CONTROL")) {
  82.                 String ControlText =dis.readUTF();
  83.                 String[] Control = ControlText.split(":");
  84.                 Log.d(profesor.LOG_TAG,"S: Recibido nuevo progreso de prueba. IP: "+Alumnos.getIdAlumno(ClientAddr)+"->"+Integer.parseInt(Control[0])+" ->"+Integer.parseInt(Control[1]));
  85.  
  86.                 Alumnos.setProgress(Alumnos.getIdAlumno(ClientAddr), Integer.parseInt(Control[0]), Integer.parseInt(Control[1]));
  87.  
  88.  
  89.                 /****************************************************/
  90.                 //Here is where I need to call the update method of the
  91.                 //class teacher to delete the old data and fill the table
  92.                 //again.
  93.                 /****************************************************/
  94.             }  
  95.             dis.close();
  96.             sock.close();
  97.         }
  98.  
  99.         } catch (IOException e) {
  100.             Log.d(profesor.LOG_TAG, "IOException"+e);
  101.             e.printStackTrace();
  102.         }
  103.     }
  104. }
  105.        
  106. refHandler.post(new Runnable(){
  107.     public void run() {
  108.         //Update
  109.     }
  110. });
  111.  
  112. refActivity.runOnUiThread(new Runnable() {
  113.     public void run() {
  114.        //Update
  115.     }
  116. });
  117.        
  118. // RECEIVING CONTROL MESSAGE
  119.             if (connectionType.contains("CONTROL")) {
  120.                 String ControlText =dis.readUTF();
  121.                 String[] Control = ControlText.split(":");
  122.                 Log.d(profesor.LOG_TAG,"S: Recibido nuevo progreso de prueba. IP: "+Alumnos.getIdAlumno(ClientAddr)+"->"+Integer.parseInt(Control[0])+" ->"+Integer.parseInt(Control[1]));
  123.  
  124.                 Alumnos.setProgress(Alumnos.getIdAlumno(ClientAddr), Integer.parseInt(Control[0]), Integer.parseInt(Control[1]));
  125.  
  126.  
  127.                 /****************************************************/
  128.                 //Here is where I need to call the update method of the
  129.                 //class teacher to delete the old data and fill the table
  130.                 //again.
  131.                 /****************************************************/
  132.     profesor.this.runOnUiThread(new Runnable() {
  133.         @Override
  134.         public void run() {
  135.             // refresh ui here
  136.         }
  137.     });
  138.         }