Activity子線程修改UI

創建子線程通過http獲取數據後,如何讓數據顯示在UI上,顯然,直接在子線程中修改是不可以的,移至線程外,通過成員變量修改也是不行的,通過學習,嘗試,發現handler能實現這一功能。通過handler接收子線程獲取的數據,並修改UI;子線程通過handler把數據往外傳,具體代碼如下:

package com.belle.apptest;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import com.belle.db.Record;
import com.belle.http.HttpUtil;
import com.belle.tools.PubUtil;

import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author yangyu 功能描述:第二種實現方式,Activity實現方式
 */
public class QueryTradeActivity extends Activity {
	// 定義標題欄上的按鈕
	private TextView name,sfz,sbkh;
	private Button ret;
	private String mesg = "";
	private String sfzh = "";
	private String cjxm = "";
	private String cjsfz="";
	private String kid = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉標題欄
		setContentView(R.layout.activity_message);
		Log.v("test", "come");

		name = (TextView) findViewById(R.id.name);
		sfz = (TextView) findViewById(R.id.sfz);
		sbkh = (TextView) findViewById(R.id.sbkh);
		ret = (Button) findViewById(R.id.ret);
		sfzh = PubUtil.searchSFZ(getApplicationContext()).getSfz();		
		Log.i("sfzh",sfzh);
		
		MyHandler mhandler = new MyHandler(name,sfz,sbkh);
		
		MyThread mthread = new MyThread(mhandler);
		
		new Thread(mthread).start();

		ret.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(QueryTradeActivity.this, MainActivity.class);
				startActivity(intent);
				QueryTradeActivity.this.finish();
				overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
			}
		});	
		
	}
	
	public class MyHandler extends Handler{
		private TextView name;
		private TextView sfz;
		private TextView sbkh;
		
		public MyHandler(TextView tv1,TextView tv2,TextView tv3){
			this.name = tv1;
			this.sfz = tv2;
			this.sbkh = tv3;
		}
		
		public void handleMessage(Message msg){
			super.handleMessage(msg);
			Bundle bundle = msg.getData();
			Log.i("bundle",bundle.toString());
			HashMap map = PubUtil.annalyzeJSON(bundle.getString("personInfo"));
			name.setText(PubUtil.nullToEmpty(map.get("cjxm")+""));
			sfz.setText(PubUtil.nullToEmpty(map.get("cjsfz")+""));
			sbkh.setText(PubUtil.nullToEmpty(map.get("kid")+""));
		}
	}
	
	public class MyThread implements Runnable{
		MyHandler handler;
		public MyThread(MyHandler handler){
			this.handler = handler;
		}
		
		public void run(){
			try{
				String url = HttpUtil.BASE_URL + "queryMessageServlet";
				mesg = HttpUtil.queryStringForPost(url,sfzh);
				Log.i("mesg",mesg);
			}catch(Exception e){
				Log.i("Register",e.toString());
			}
			
			Message msg = new Message();
			Bundle bundle = new Bundle();
			
			bundle.putString("personInfo", mesg);
			msg.setData(bundle);
			
			handler.sendMessage(msg);
		}
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章