异步加载package com.tianmg.AsynTask;import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.MalformedURLException;import java.net.URI;import java.net.URL;import java.util.ArrayList;import java.util.List;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import com.example.asyntask.R;import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;import android.util.Log;import android.widget.ListView;public class AsyntaskOfListView extends Activity { ListView listview; static String url="http://www.tianmg.com/api/teacher?type=4&num=30"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_listview); listview=(ListView) findViewById(R.id.listview); AnysTask task=new AnysTask(); task.execute(url); }public List getJsongetJson(String url){ List newsBeanList=new ArrayList(); try { String jdata=readStream(new URL(url).openStream()); JSONObject jsonObject; newsBean bean; try { jsonObject=new JSONObject(jdata); JSONArray jsonArray=jsonObject.getJSONArray("data"); for (int i = 0; i < jsonArray.length(); i++) { bean=new newsBean(); jsonObject=jsonArray.getJSONObject(i); bean.image=jsonObject.getString("picSmall"); bean.title=jsonObject.getString("name"); bean.content=jsonObject.getString("description"); newsBeanList.add(bean); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return newsBeanList; }public String readStream(InputStream is){ String result = null; try { String line=""; InputStreamReader isr=new InputStreamReader(is, "utf-8"); BufferedReader br=new BufferedReader(isr); while((line=br.readLine())!=null){ result+=line; } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } class AnysTask extends AsyncTask{ @Override protected List doInBackground(String... params) { // TODO Auto-generated method stub return getJsongetJson(params[0]); } @Override protected void onPostExecute(List result) { // TODO Auto-generated method stub super.onPostExecute(result); NewsAdapter adapter=new NewsAdapter(result,AsyntaskOfListView.this); listview.setAdapter(adapter); } }}适配器package com.tianmg.AsynTask;//适配器import java.util.List;import com.example.asyntask.R;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;public class NewsAdapter extends BaseAdapter{ private List newsbeanlist; private LayoutInflater inflater; public NewsAdapter(List newsbeanlist1,Context context){ this.newsbeanlist=newsbeanlist1; inflater=LayoutInflater.from(context); } @Override public int getCount() { // TODO Auto-generated method stub return newsbeanlist.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return newsbeanlist.get(arg0); } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public View getView(int arg0, View convertview, ViewGroup arg2) { // TODO Auto-generated method stub ViewHolder viewHolder=null; if(convertview==null){ viewHolder=new ViewHolder(); convertview=inflater.inflate(R.layout.item, null); viewHolder.image=(ImageView) convertview.findViewById(R.id.imageview); viewHolder.title=(TextView) convertview.findViewById(R.id.title); viewHolder.content=(TextView) convertview.findViewById(R.id.content); convertview.setTag(viewHolder); } else{ viewHolder=(ViewHolder) convertview.getTag(); } viewHolder.image.setImageResource(R.drawable.ic_launcher); viewHolder.title.setText(newsbeanlist.get(arg0).title); viewHolder.content.setText(newsbeanlist.get(arg0).content); return convertview; } class ViewHolder{ public TextView title; public TextView content; public ImageView image; }}定义的泛型package com.tianmg.AsynTask;public class newsBean { public String image; public String title; public String content;} |