我是 Android 新手,发现要不断更新主视图,我必须创建一个线程来处理各种进程,然后将更新传递回主视图。我决定使用 Handler 类来做到这一点。此示例中的视图有一个用于激活代码的按钮和一个用于显示接收到的消息的表格布局 - 代表流程的一个阶段。
问题是我发送了 8 条不同的消息,我可以看到 8 条消息在处理程序中返回,但所有 8 条消息的内容仅与消息 8 相同。
我期待处理程序按顺序接收消息。
任何关于更好的方法的建议总是欢迎学习。
这是代码:
`
public class messageHandlerTest extends Activity {
protected TextView textView;
protected Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
String[] status = (String[]) msg.obj;
createTableRow(status);
Log.e("Got a new message",status[0]+":"+status[1]);
}
};
Button btnStartProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.statusText);
textView.setText("");
//Getting response from server with Network SSID and Password
Button connectButton = (Button)findViewById(R.id.connectButton);
addListenerOnButton();
} // End of create
protected class connectWiFi extends Thread implements Runnable {
//tokens1 = new String[0];
public void run(){
try {
String[] messageString = new String[2];
Message message = handler.obtainMessage();
messageString[0]="OK";
messageString[1]="Number 1";
message.obj = messageString;
handler.sendMessage(message);
message = handler.obtainMessage();
messageString[0]="OK";
messageString[1]= "Number 2";
message.obj = messageString;
handler.sendMessage(message);
message = handler.obtainMessage();
messageString[0]="OK";
messageString[1] = "Number 3";
message.obj = messageString;
handler.sendMessage(message);
message = handler.obtainMessage();
messageString[0]="OK";
messageString[1] = "Number 4";
message.obj = messageString;
handler.sendMessage(message);
message = handler.obtainMessage();
messageString[0] = "OK";
messageString[1] = "Number 5";
message.obj = messageString;
handler.sendMessage(message);
message = handler.obtainMessage();
messageString[0] = "OK";
messageString[1] = "Number 6";
message.obj = messageString;
handler.sendMessage(message);
message = handler.obtainMessage();
messageString[0] = "OK";
messageString[1] = "Number 7";
message.obj = messageString;
handler.sendMessage(message);
message = handler.obtainMessage();
messageString[0] = "OK";
messageString[1] = "Number 8";
message.obj = messageString;
handler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
Log.e("Exception found","bugger");
}
}// End or run
}// End of class
public void addListenerOnButton() {
btnStartProgress = (Button) findViewById(R.id.connectButton);
btnStartProgress.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
connectWiFi connectwifi = new connectWiFi();
connectwifi.start();
}
});
}
private void createTableRow(String[] stage) {
TableLayout tl = (TableLayout) findViewById(R.id.statusTable);
TableRow tr = new TableRow(this);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
tr.setBackgroundColor(0xFFFFFFFF);
TextView tvStatus = new TextView(this);
tvStatus.setLayoutParams(lp);
tvStatus.setPadding(2,1,1,2);
tvStatus.setTextColor(0xFF000000);
tvStatus.setText(stage[0]); // Status
TextView tvStage = new TextView(this);
tvStage.setLayoutParams(lp);
tvStage.setPadding(2,1,1,2);
tvStage.setTextColor(0xFF000000);
tvStage.setText(stage[1]); // Stage
tr.addView(tvStatus);
tr.addView(tvStage);
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
此代码的结果如下所示: