我遇到了一个问题,按下按钮我尝试读取有数据进入的 DataInputstream 并显示数据。
我正在使用 while 循环来读取数据。但是 Textview 的动态更新不会发生。
TextView datatextview = (TextView)findViewById(R.id.data);
DataInputStream Din = new DataInputStream(socket.getInputStream());
Button getData= (Button)findViewById(R.id.getdata);
getData.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//.......stuff .......
try{
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int bytesRead = -1;
String message1 = "";
while (true) {
message1 = "";
data = Din.readLine();
bytesRead = (reading).length();
if (bytesRead != -1) {
Log.v(TAG,"data"+data); //I'm getting the data correctly
//But not able to update it in the TextView :(
datatextview.setText(data); //doesnt work
}
}