Pass String to Activity
<p>I am new to Android development and Java and was wondering if somebody could help me with the following:</p>

<p>I have created an application that runs a server thread listening on a specified port. I would like to print messages received from a connected client into a TextView in the activity.</p>

<p>The server thread is in a separate class. The run method in this class listens for a client connection and reads any data received into a String. </p>

<p>What would be the best way for me to transfer the contents of this String back to the activity so that it can update the TextView?</p>

<p>From my (limited) understanding, only the ui thread should update a TextView and I can't find a way to get runOnUiThread to update the TextView. </p>

<p>Added code as requested.</p>

<p>Activity code:</p>

<pre><code>public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView messages = (TextView) findViewById(R.id.messages);

    try {
        newThread server = new newThread(this, messages);
    } catch(Exception e) {
        Toast.makeText(ChatActivity.this, e.toString(), Toast.LENGTH_LONG).show();
    }

}
</code></pre>

<p>Run method in newThread class:</p>

<pre><code>public void run()
{
    serv = new ServerSocket(8000);
    while(true)
    {
        cli = serv.accept();
        user = cli.getInetAddress().toString();
        BufferedReader cli_in = new BufferedReader(new InputStreamReader(cli.getInputStream()));
        OutputStreamWriter cli_out = new OutputStreamWriter(cli.getOutputStream());

        while((buf = cli_in.readLine()) != null)
        {
            // Update the messages TextView with buf 
        }

     }
}
</code></pre>

<p>To avoid making things too cluttered I have omitted what irrelevant code I can. </p>

<p>Basically, in the inner while loop in run() I would like to pass the "buf" String to the activity so that the messages textview can be updated with it's content. </p>

<p>Cheers</p>
