设为首页 加入收藏

TOP

使用异步任务加载网络上json数据并加载到ListView中(三)
2017-10-13 10:35:52 】 浏览:6050
Tags:使用 异步 任务 加载 网络 json 数据 ListView
面的布局中加入一个ListView控件,再定义一个item.xml用来作为ListView的自布局,主界面就不上代码了,下面看一下item的布局:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="wrap_content">
 5 
 6     <TextView
 7         android:id="@+id/tv_title"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:padding="10dp"
11         android:text="Title"
12         android:textSize="20sp" />
13 
14     <TextView
15         android:id="@+id/tv_summary"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:layout_below="@+id/tv_title"
19         android:padding="10dp"
20         android:text="Summary"
21         android:textSize="12sp" />
22 
23     <TextView
24         android:id="@+id/tv_id"
25         android:layout_width="wrap_content"
26         android:layout_height="wrap_content"
27         android:layout_alignParentRight="true"
28         android:layout_alignTop="@+id/tv_summary"
29         android:padding="10dp"
30         android:text="id"
31         android:textSize="12sp" />
32 
33 </RelativeLayout>

   基本工作都完成了,下面完成异步任务类DownloadAsyncTask中的内容,因为在进入后台线程前没有什么准备工作,并且也不需要进度条,所以就只重写了doInBackground()方法和onPostExecute()方法,代码如下:

 1 package com.yztc.lx.asynctasklistview.com.yztc.lx.async;
 2 
 3 import android.content.Context;
 4 import android.os.AsyncTask;
 5 import android.util.Log;
 6 import android.widget.ListView;
 7 import android.widget.Spinner;
 8 import android.widget.Toast;
 9 
10 import com.yztc.lx.asynctasklistview.com.yztc.lx.adapter.MyBaseAdapter;
11 import com.yztc.lx.asynctasklistview.com.yztc.lx.bean.News;
12 import com.yztc.lx.asynctasklistview.com.yztc.lx.utils.HttpUtils;
13 import com.yztc.lx.asynctasklistview.com.yztc.lx.utils.ParserJson;
14 
15 import java.util.List;
16 
17 /**
18  * Created by Lx on 2016/8/10.
19  */
20 
21 public class DownloadAsyncTask extends AsyncTask<String, Void, List<News>> {
22     private Context mContext;
23     private ListView lv;
24     private Spinner sp;
25 
26     public DownloadAsyncTask(Context mContext, ListView lv) {
27         this.mContext = mContext;
28         this.lv = lv;
29     }
30 
31 
32     @Override
33     protected List<News> doInBackground(String... params) {
34         List<News> list = null;
35         if(HttpUtils.isNetConn(mContext)){
36             byte[] b=HttpUtils.downloadFromNet(params[0]);  //可变参数params当成一个数组使用,其中的params[0]就是我们传递过来的参数
37             String jsonString=new String(b);
38             Log.d("Tag",jsonString);
39             list=ParserJson.parserJsonToNews(jsonString);
40             Log.d("List",list.toString());
41         }
42         return list;
43     }
44 
45     @Override
46     protected void onPostExecute(List<News> newses) {
47         if(newses!=null&&newses.size()!=0){
48             MyBaseAdapter adapter=new MyBaseAdapter(mContext,newses);
49             lv.setAdapter(adapter);
50         }else {
51             Toast.makeText(mContext,"数据加载失败", To
首页 上一页 1 2 3 4 5 下一页 尾页 3/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Android AsyncTask 深度理解、简.. 下一篇实用控件分享:自定义逼真相机光..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目