从网络上异步加载数据,显示到ListView上

原文链接:http://www.xiaoyuanbiji.top/319.html点击打开链接

使用AsyncTask异步类从网络上加载数据,并显示到listview上

直接上代码了
MainActivity.java

public class MainActivity extends Activity {
 
    private MyTask mTask;
    ListView listView;
    SimpleAdapter adapter;
    List<HashMap<String, String>> data;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) this.findViewById(R.id.listView1);
 
        data = new ArrayList<HashMap<String, String>>();
 
        //创建SimpleAdapter适配器并将数据绑定到item显示控件上
        // this表示访问整个android应用程序接口,基本上所有的组件都需要
        adapter = new SimpleAdapter(this,
                //data就是要显示的数据,List<HashMap<String, String>> data;
                data, 
                //每一项的布局文件(就是每一行的布局文件)
                R.layout.item, 
                 
                //我们的data中保存的是HashMap的对象,但是怎么样指定数据显示在哪个控件上呢?这就是下面两个数组的功能了
                //这个字符串数组就是HashMap中的键,每个键对应的值显示在int[]数组对应下标指定的控件上
                //也就是说Cons.NAME对应的值会显示在R.id.name这个控件上
                new String[] { Cons.NAME, Cons.PRICE,Cons.AVERPRICE, Cons.CHANGE }, 
                //控件的ID数组
                new int[] { R.id.name,R.id.price, R.id.averprice, R.id.change});
        listView.setAdapter(adapter);
         
        //异步类,继承了AsyncTask实例必须在UI thread中创建;
        mTask = new MyTask();
        //执行异步任务,注意这里不是调用doInBackground()这个方法
        //execute方法必须在UI thread中调用
        mTask.execute("传递给doInBackground()的参数");
 
    }
     
    /**
     *  内部类
     *  AsyncTask是android提供的轻量级的异步类,使用简单
     *  < >中的第一个数据类型:doInBackground()传入的数据类型
     *  < >中的第二个数据类型:onProgressUpdate()传入的数据类型,这个方法大多用来更新进度条
     *  < >中的第三个数据类型:onPostExecute()传入的数据类型
     */
    private class MyTask extends AsyncTask<String, Integer, List<HashMap<String, String>>> {
 
        /**
         * doInBackground方法内部执行后台任务,不可在此方法内修改UI
         * 该方法执行完成后,返回的参数就是onPostExecute()传入的参数
         */
        @Override
        protected List<HashMap<String, String>> doInBackground(String... params) {
            //publishProgress(i)更新进度条
            //从服务器上下载数据,这里的类需要自己写
            return GetInfo.getJSONObject(Helper.getJson(params[0]));
        }
 
        /**
         * onPostExecute方法用于在执行完后台任务后更新UI,显示结果
         */
        @Override
        protected void onPostExecute(List<HashMap<String, String>> result) {
            for (HashMap<String, String> li : result) {
                data.add(li);
            }
            //将下载的数据添加到list容器后,刷新每个Item的内容
            adapter.notifyDataSetChanged();
            super.onPostExecute(result);
        }
        /**
         * 用来更新进度条的
         * 每次调用publishProgress(i)都会触发onProgressUpdate执行, i就是values 
         */
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
        }
    }
 
}

item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="80dp"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/name"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:gravity="center"
        android:textSize="20sp" />
    <TextView
        android:id="@+id/price"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="4"
        android:gravity="center"
        android:textSize="20sp" />
    <TextView
        android:id="@+id/averprice"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:gravity="center"
        android:textSize="20sp" />
    <TextView
        android:id="@+id/change"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:textSize="20sp" />
</LinearLayout>


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值