关于异步加载数据的又一种实现
2012-10-07 16:40
543 查看
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); } }; }
相关文章推荐
- 关于异步加载数据的又一种实现
- 关于cocos2d-x进度条的实现和异步加载资源
- (Android实战)ProgressBar+AsyncTask实现界面数据异步加载(含效果图)
- 异步实现,查询大量数据时的加载
- Android进阶——借助Loader机制以一种更优美的方式异步加载数据(一)
- Android_Loader_使用LoaderManager管理Loader实现异步动态加载数据
- 用tableView实现的一种加载数据的布局
- zTree实现打开页面时异步加载数据及选中项ID提交到后台
- 打造Android集合控件数据绑定(支持添加监听,支持AbsListView与RecycleView,支持异步加载等)(三)具体实现
- ECharts3 实现 AJAX异步加载 数据库数据
- Windows Phone 利用Async CTP实现异步加载数据
- 关于Ext3.0中按条件查询并重新加载Grid中的数据的实现过程及store.load的分析
- 如何使用PHP+jQuery+MySQL实现异步加载ECharts地图数据(附源码下载)
- ztree实现异步加载(点击节点,请求后台数据,添加数据到对应节点)
- LoaderManager管理Loader实现异步动态加载数据,并使用RecyclerView 显示
- struts2+jquery+json实现异步加载数据(自写)
- 转载 (Android实战)ProgressBar+AsyncTask实现界面数据异步加载
- EASYUI TREEGRID异步加载数据实现方法
- 如何使用PHP+jQuery+MySQL实现异步加载ECharts地图数据(附源码下载)
- jQuery+Ajax滚屏异步加载数据实现(附源码)