-1

before writing this I looked at other post but have not found any solution, I would appreciate your help.

In the onCreate method of the class of the application launcher creates a thread TCPServer and display a table with information. The problem is that this information varies and must be updated when the thread detects TCPServer have sent a new message control.

Then I show the code if I expressed myself well.

//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();
        }
    }
}

Can anyone give me some idea?, I have seen examples handlers but not in a class declaration and the call in another.

I hope that you understand what I mean, my English is not very good.

Thanks for the help

  • Welcome to stackoverflow, please search SO before posting. this has been discussed on numerous ocassions, e.g. http://stackoverflow.com/questions/4369537/update-ui-from-thread – Orlymee Jun 22 '12 at 15:36

2 Answers2

1

You can try to create the TCPServer with a Handler or Activity references and then use it when you want to update the UI:

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

refActivity.runOnUiThread(new Runnable() {
    public void run() {
       //Update
    }
});
  • Thanks for the quick response, but I tried to do these two forms and does not get it to work. Eclipse does not recognize the refActivity in my case profesor.this. if under the static method then updated as Eclipse gives "setContentView (R.layout.main)" failure in the updated method of the class profesor. I dont know – user1177917 Jun 22 '12 at 16:13
  • From what I've seen, the problem is that the context is lost when I'm on Thread TCP. How I can to safeguard the context is not lost in the passage of the thread to the class profesor? Thank you – user1177917 Jun 25 '12 at 09:07
0

use runOnUiThread for updating ui from Thread:

// 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
        }
    });
        }   
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • To do it that way, there is an error (No enclosing instance of the type profesor is accessible in scope) on profesor.this. In making the call to profesor.actualiza () public void run into the following error (Can not make a static reference to the non-static method actualizarPantalla () from the type profesor). The problem is that if the frame as a static method method update, then gives the same error in the setContentView (R.layout.main) update method of the class profesor. I'm going crazy – user1177917 Jun 22 '12 at 16:37
  • 1
    From what I've seen, the problem is that the context is lost when I'm on Thread TCP. How I can to safeguard the context is not lost in the passage of the thread to the class profesor? Thank you – user1177917 Jun 25 '12 at 09:04