关于异步加载数据的又一种实现
2012-10-07 16:40
459 查看
package com.testasyntextview; /** * 把获取的线程写到方法中(比较好) */ import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.Html; import android.text.Spanned; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class TestAsynTextViewActivity extends Activity { private TextView textView1; private Button button1; private Context context; private ProgressDialog progressDialog; private Spanned html; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); context = this; textView1 = (TextView) findViewById(R.id.textView1); button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(l); } private OnClickListener l = new OnClickListener() { @Override public void onClick(View v) { progressDialog = ProgressDialog.show(context, "获取数据中", "等待"); getHtmlDate(); } }; private void getHtmlDate() {// 获取数据,把线程写入了其中 new Thread() { public void run() { Message msg = myHandler.obtainMessage(); try { html = HttpUtil.fromHtml(HttpUtil .getHtml("http://wap.sina.com")); msg.what = 0; } catch (Exception e) { e.printStackTrace(); msg.what = 1; } myHandler.sendMessage(msg); } }.start(); } Handler myHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 0: textView1.setText(html); progressDialog.dismiss(); break; case 1: textView1.setText("当前无数据"); progressDialog.dismiss(); break; } super.handleMessage(msg); } }; }
相关文章推荐
- 关于异步加载数据的又一种实现
- ztree插件的使用及列表项拖拽的实现(jQuery)+异步加载节点数据
- Ajax 之使用 XMLHttp实现简单的异步数据加载
- C#使用Jquery zTree实现树状结构显示 异步数据加载
- Asp.net TreeView异步加载数据,并结合checkbox实现多选删除
- 异步实现,查询大量数据时的加载
- Android异步加载学习笔记之二:实现ListView中的图片数据从网络加载
- Android_Loader_使用LoaderManager管理Loader实现异步动态加载数据
- 微信小程序中使用ECharts 异步加载数据实现图表功能
- (Android实战)ProgressBar+AsyncTask实现界面数据异步加载(含效果图)
- extjs 3.x简单的异步树 并实现展开时重新加载数据的实现
- 异步方式实现数据加载
- java +jsp实现的点击加载更多,实现数据的异步加载
- (Android实战)ProgressBar+AsyncTask实现界面数据异步加载(含效果图)
- EASYUI TREEGRID异步加载数据实现方法
- C#使用Jquery zTree实现树状结构显示_异步数据加载
- AndroidのLoaderManager管理Loader实现异步动态加载数据
- Android进阶——借助Loader机制以一种更优美的方式异步加载数据(一)
- android结合异步任务,动态加载图片,Json解析数据展示在ListView,并且实现按日期分类展示,借口回调
- Android Loader机制,实现异步加载数据