执行后TextView不会追加

时间:2012-05-10 09:47:48

标签: android android-asynctask progressdialog textview

你可以看到我在onPostExecute中附加了tvStatus(TextView)并删除了我的progressDialog框。当我调试我的代码时,我可以看到该值是在tvStatus中设置的,但它没有在屏幕上显示。

在调用onPostExecute函数后,我的progressDialog也会停止旋转。 有谁知道为什么以及如何解决?

这是在onCreate方法中设置的:

tvStatus = (TextView) this.findViewById(R.id.tvStatus);

代码:

public class TcpTask extends AsyncTask<Void, Void, Integer> {

    @Override
    protected Integer doInBackground(Void... params) {
        try {
            //set up a Connection
            Socket s = new Socket("88.26.249.133", TCP_SERVER_PORT);
            InputStream inputstream = (s.getInputStream());
            DataInputStream in = new DataInputStream(inputstream);
            DataOutputStream out = new DataOutputStream(s.getOutputStream());
            s.setSoTimeout(20*1000);
            //prepare output message
            outBuffer[0] = 48;
            outBuffer[1] = 51;
            outBuffer[2] = 49;
            outBuffer[3] = 49;
            outBuffer[4] = 0;
            outBuffer[5] = 0;
            //send output message
            out.write(outBuffer);
            out.flush();
            //To check in logCat
            Log.i("TcpTask", "sent: " + outBuffer);
            //check # available data
            //and use it as byte length
            avail = in.available();
            byte[] inBuffer = new byte[avail];
            //accept server response
            while ((nob = in.read(inBuffer)) != -1) {
            }
            //close stream
            in.close();
            for (int i = 7; i < avail-7; i += 2) {
                lowByte = inBuffer[i];
                highByte = inBuffer[i+1];
                if (lowByte < 0) {
                    result = lowByte + 256 + (highByte * 256);
                } else {
                    result = lowByte + (highByte * 256);
                }
            }
            //close connection
            s.close();
            //To Check in logCat
            Log.i("TcpTask", "received: " + inBuffer);
            // if the host name could not be resolved into an IP address.   
        } catch (UnknownHostException e) {
            e.printStackTrace();
            Log.i("myStatus", "TcpClient: Host name could not be resolved");
            // if an error occurs while creating the socket.
        } catch (IOException e) {
            e.printStackTrace();
            Log.i("myStatus", "TcpClient: ERROR");
        } finally {
            Log.i("TcpTask", "TCPClient: Finished");
        }
        return result;
    }

    @Override
    protected void onPostExecute(Integer result) {
        tvStatus.append(Integer.toString(result) + System.getProperty("line.separator"));
        if (myStatus.this.progDialog != null) {
            myStatus.this.progDialog.dismiss();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

tvStatus.setText(tvStatus.getText().toString+(Integer.toString(result) + System.getProperty("line.separator")));

这是将Text值设置为textView

的方法

答案 1 :(得分:0)

使用

tsStatus.setText(result+" "+ System.getProperty("line.separator");