使用Handler加载网络图片,设置ListView
加载网络图片生成listview,通过打开网络连接,通过异步线程处理网络加载,并设置图片
MainActivity代码
连接网络,解析JSON代码,将所解析出的Json对象封存,添加到List中并与之后自定义的Adapter关联
public class MainActivity extends AppCompatActivity { private List<Newsbeans> mlist; private static String url="http://www.imooc.com/api/teacher?type=4&num=30"; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Myaysctask myaysctask=new Myaysctask(); //将url传递进去实现对于url的异步访问 new Myaysctask().execute(url); listView= (ListView) findViewById(R.id.listview); } //将url转化为我们所需要的json对象 private List<Newsbeans> getjsondata(String url){ //初始化list mlist=new ArrayList<>(); JSONObject jsonObject=null; Newsbeans newsbeans=new Newsbeans(); try { //获取字节流 String jsonstring= readStream(new URL(url).openStream()); //将传入的字节流转化为json对象 jsonObject=new JSONObject(jsonstring); //得到json下的data下的数组 JSONArray jsonArray=jsonObject.getJSONArray("data"); for(int i=0;i<jsonArray.length();i++){ //遍历json数组的到jsonobject的值 jsonObject=jsonArray.getJSONObject(i); newsbeans=new Newsbeans(); //将jsonobject中的值赋予到已经封装好的值之中 newsbeans.newscontent=jsonObject.getString("description"); newsbeans.newspic=jsonObject.getString("picSmall"); newsbeans.newstitle=jsonObject.getString("name"); //将newsbeans添加到list之中 mlist.add(newsbeans); } } catch (IOException | JSONException e) { e.printStackTrace(); } return mlist; } private String readStream(InputStream is){ String result=""; String line=""; try { //将inputstream传入的为字节流,通过inputstreamreader将字节流转换为字符流 InputStreamReader irs=new InputStreamReader(is,"utf-8"); //通过bufferedreader将输入流读取出来 BufferedReader br= new BufferedReader(irs); while ((line= br.readLine())!=null){ result+=line; } } catch (IOException e) { e.printStackTrace(); } return result; } class Myaysctask extends AsyncTask<String,Void,List<Newsbeans>>{ @Override //进行所有的耗时操作但不包括更改UI protected List<Newsbeans> doInBackground(String... strings) { //将string数组中的0位返回也就是传入url return getjsondata(strings[0]); } @Override
//加载所需的adapter protected void onPostExecute(List<Newsbeans> newsbeans) { Myadapter myadapter=new Myadapter(MainActivity.this,newsbeans); listView.setAdapter(myadapter); super.onPostExecute(newsbeans); } } }
使用高效的adapter,关联listview
1 public class Myadapter extends BaseAdapter { 2 private Context context; 3 private List<Newsbeans> mlist; 4 private LayoutInflater mlayoutinflater; 5 public Myadapter(Context context, List<Newsbeans> mlist1){ 6 mlist=mlist1; 7 mlayoutinflater=LayoutInflater.from(context); 8 } 9 @Override 10 public int getCount() { 11 12 return mlist.size(); 13 } 14 15 @Override 16 public Object getItem(int i) { 17 return mlist.get(i); 18 } 19 20 @Override 21 public long getItemId(int i) { 22 return i; 23 } 24 25 @Override 26 public View getView(int i, View convertview, ViewGroup viewGroup) { 27 Viewholder viewholder=null; 28 if(convertview==null){ 29 viewholder=new Viewholder(); 30 //将item的layout文件转化为view,必须是item文件在之后的view中还要找到对应的控件 31 convertview=mlayoutinflater.inflate(R.layout.item,null); 32 viewholder.contenttext= (TextView)convertview.findViewById(R.id.tv_content); 33 viewholder.imageView=(ImageView)convertview.findViewById(R.id.imageview); 34 viewholder.titletext=(TextView)convertview.findViewById(R.id.tv_title); 35 convertview.setTag(viewholder); 36 } 37 else { 38 viewholder=(Viewholder)convertview.getTag(); 39 } 40 Newsbeans newsbeans=mlist.get(i); 41 viewholder.imageView.setImageResource(R.mipmap.ic_launcher); 42 //调用方法传递所需信息 43 new ImageviewHolder().newthread(viewholder.imageView,mlist.get(i).newspic); 44 viewholder.imageView.setTag(mlist.get(i).newspic); 45 viewholder.titletext.setText(newsbeans.newstitle); 46 viewholder.contenttext.setText(newsbeans.newscontent); 47 return convertview; 48 } 49 class Viewholder{ 50 public TextView titletext; 51 public TextView contenttext; 52 public ImageView imageView; 53 } 54 }
使用Handler加载网络图片
1 public class ImageviewHolder { 2 //设置全局变量 3 private ImageView mImageView; 4 private String mURL; 5 //定义handler,在handler之中设定UI 6 private android.os.Handler mHadler= new android.os.Handler(){ 7 @Override 8 public void handleMessage(Message msg) { 9 //由于Listview的缓存属性,在设加载UI的时候可能会出现重复加载的情况 10 if(mImageView.getTag().equals(mURL)){ 11 mImageView.setImageBitmap((Bitmap) msg.obj); 12 } 13 14 super.handleMessage(msg); 15 } 16 }; 17 public void newthread(ImageView imageView, final String url){ 18 mImageView=imageView; 19 mURL=url; 20 //进行所有的耗时操作 21 new Thread(){ 22 @Override 23 public void run(){ 24 try { 25 Thread.sleep(2000); 26 Message message=new Message(); 27 //传递信息,将message的obj设置为一个bitmap并传递给handler设置UI 28 message.obj=getbitmapstream(url); 29 mHadler.sendMessage(message); 30 } catch (IOException | InterruptedException e) { 31 e.printStackTrace(); 32 } 33 super.run(); 34 } 35 }.start(); 36 } 37 public Bitmap getbitmapstream(String bitmapurl) throws IOException { 38 InputStream is=null; 39 try { 40 //指向互联网资源的指针 41 URL url=new URL(bitmapurl); 42 //打开连接,并强行转换格式 43 HttpURLConnection connection= (HttpURLConnection) url.openConnection(); 44 //获取输入的数据流 45 is=new BufferedInputStream(connection.getInputStream()); 46 //解析所传入的数据 47 Bitmap bitmap= BitmapFactory.decodeStream(is); 48 //断开连接 49 connection.disconnect(); 50 return bitmap; 51 } catch (IOException e) { 52 e.printStackTrace(); 53 } 54 finally { 55 //断开数据流 56 assert is != null; 57 is.close(); 58 } 59 return null; 60 } 61 }