你的位置:首页 > 操作系统

[操作系统]网络加载图片 ListView


 1 <??> 2 <LinearLayout ="http://schemas.android.com/apk/res/android" 3   android:layout_width="match_parent" 4   android:layout_height="wrap_content" 5   android:orientation="horizontal" 6   android:padding="5dp"> 7  8   <ImageView 9     android:id="@+id/newsIcon"10     android:layout_width="64dp"11     android:layout_height="64dp"12     android:layout_marginRight="5dp"13     android:src="@mipmap/ic_launcher" />14 15   <LinearLayout16     android:layout_width="match_parent"17     android:layout_height="wrap_content"18     android:layout_gravity="center"19     android:orientation="vertical">20 21     <TextView22       android:id="@+id/tittle"23       android:layout_width="wrap_content"24       android:layout_height="match_parent"25       android:maxLines="1"26       android:text="Tittle"27       android:textSize="15dp" />28 29     <TextView30       android:id="@+id/content"31       android:layout_width="match_parent"32       android:layout_height="wrap_content"33       android:maxLines="3"34       android:text="content"35       android:textSize="12dp" />36   </LinearLayout>37 38 39 </LinearLayout>

View Code

 

Adapter

 1 package com.test.fin4l.test; 2  3 import android.content.Context; 4 import android.view.LayoutInflater; 5 import android.view.View; 6 import android.view.ViewGroup; 7 import android.widget.BaseAdapter; 8 import android.widget.ImageView; 9 import android.widget.TextView;10 11 import java.util.List;12 13 /**14  * Created by Fin4l on 2016/8/9.15 */16 17 public class MyAdapter extends BaseAdapter {18   private List<NewBean> mLists;19   private LayoutInflater inflater;20 21 22 23   public MyAdapter(Context context,List<NewBean> lists) {24     inflater=LayoutInflater.from(context);25     mLists=lists;26 27   }28 29   @Override30   public int getCount() {31     return mLists.size();32   }33 34   @Override35   public View getView(int position, View convertView, ViewGroup parent) {36     ViewHolder vh;37     if (convertView == null) {38       convertView=inflater.inflate(R.layout.news_item,parent,false);39       vh=new ViewHolder();40       vh.tittleTV= (TextView) convertView.findViewById(R.id.tittle);41       vh.contentTV= (TextView) convertView.findViewById(R.id.content);42       vh.imgView= (ImageView) convertView.findViewById(R.id.newsIcon);43 44       convertView.setTag(vh);45 46     } else {47       vh= (ViewHolder) convertView.getTag();48 49     }50     vh.tittleTV.setText(mLists.get(position).newsTittle);51     vh.contentTV.setText(mLists.get(position).newsContent);52     ImageLoader loader=new ImageLoader();53     loader.showImageByThread(mLists.get(position).iconUrl,vh.imgView);54     return convertView;55   }56 57   @Override58   public long getItemId(int position) {59     return position;60   }61 62   @Override63   public Object getItem(int position) {64     return mLists.get(position);65   }66 67   class ViewHolder {68     public ImageView imgView;69     public TextView tittleTV;70     public TextView contentTV;71 72 73   }74 }

View Code

 

Activity

 1 package com.test.fin4l.test; 2  3 import android.os.AsyncTask; 4 import android.os.Bundle; 5 import android.support.v7.app.AppCompatActivity; 6 import android.util.Log; 7 import android.widget.ListView; 8  9 import org.json.JSONArray;10 import org.json.JSONException;11 import org.json.JSONObject;12 13 import java.io.BufferedReader;14 import java.io.IOException;15 import java.io.InputStreamReader;16 import java.io.UnsupportedEncodingException;17 import java.net.URL;18 import java.util.ArrayList;19 import java.util.List;20 21 /**22  * Created by Fin4l on 2016/8/9.23 */24 25 public class newsActivity extends AppCompatActivity {26 27   private ListView newListView;28   private String url = "http://www.imooc.com/api/teacher?type=4&num=30";29 30   @Override31   public void onCreate(Bundle savedInstanceState) {32     super.onCreate(savedInstanceState);33     setContentView(R.layout.news_activity);34     newListView = (ListView) findViewById(R.id.lv);35     Log.d("line", System.currentTimeMillis() + "");36     new MyAsynctask().execute(url);37 38   }39 40 41   class MyAsynctask extends AsyncTask<String, Void, List<NewBean>> {42 43     @Override44     protected List<NewBean> doInBackground(String... params) {45 46       List<NewBean> lists = new ArrayList<>();47       try {48         String line;49         String result = "";50         InputStreamReader isr = new InputStreamReader(new URL(params[0]).openStream(), "utf-8");51         BufferedReader br = new BufferedReader(isr);52         while ((line = br.readLine()) != null) {53           result += line;54         }55         Log.d("line", result);56         JSONObject jsObj = new JSONObject(result);57         JSONArray jsArray = jsObj.getJSONArray("data");58         NewBean temp;59         JSONObject jsTemp;60         for (int i = 0; i < jsArray.length(); i++) {61           jsTemp = jsArray.getJSONObject(i);62           temp = new NewBean();63           temp.iconUrl = jsTemp.getString("picSmall");64           temp.newsTittle = jsTemp.getString("name");65           temp.newsContent = jsTemp.getString("description");66           lists.add(temp);67         }68       } catch (UnsupportedEncodingException e) {69         e.printStackTrace();70       } catch (IOException e) {71         e.printStackTrace();72       } catch (JSONException e) {73         e.printStackTrace();74       }75       return lists;76     }77 78     @Override79     protected void onPostExecute(List<NewBean> newBeen) {80       Log.d("line", System.currentTimeMillis() + "");81       MyAdapter adapter = new MyAdapter(newsActivity.this, newBeen);82       newListView.setAdapter(adapter);83       Log.d("line", System.currentTimeMillis() + "");84     }85   }86 }

View Code

ImageLoader

 1 package com.test.fin4l.test; 2  3 import android.graphics.Bitmap; 4 import android.graphics.BitmapFactory; 5 import android.os.Handler; 6 import android.os.Message; 7 import android.widget.ImageView; 8  9 import java.io.BufferedInputStream;10 import java.io.IOException;11 import java.io.InputStream;12 import java.net.HttpURLConnection;13 import java.net.MalformedURLException;14 import java.net.URL;15 16 /**17  * Created by Fin4l on 2016/8/9.18 */19 20 public class ImageLoader {21   private ImageView mImgView;22   private Handler handler=new Handler(){23     @Override24     public void handleMessage(Message msg) {25       mImgView.setImageBitmap((Bitmap)(msg.obj));26 27     }28   };29 30   public void showImageByThread(final String url,ImageView imgView){31     mImgView=imgView;32     new Thread(){33 34       @Override35       public void run() {36 37         Bitmap bitmap=getBitmapByUrl(url);38         Message msg=Message.obtain();39         msg.obj=bitmap;40         handler.sendMessage(msg);41 42       }43     }.start();44   }45 46   public Bitmap getBitmapByUrl(String urlString){47     Bitmap bitmap=null;48     try {49 50       URL mUrl=new URL(urlString);51       HttpURLConnection conn= (HttpURLConnection) mUrl.openConnection();52       InputStream is=new BufferedInputStream(conn.getInputStream());53       bitmap= BitmapFactory.decodeStream(is);54       conn.disconnect();55       is.close();56 57     } catch (MalformedURLException e) {58       e.printStackTrace();59     } catch (IOException e) {60       e.printStackTrace();61     }62 63 64 65     return bitmap;66 67   }68 69 }

View Code

待续