网络请求的图片,在Adapter(适配器)中异步的处理

添加权限
<uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * Created by 唐雪琴 on 2017/11/30.
 */
public class MyAdapter extends BaseAdapter {
    private List list;
    private Context context;
    private Bitmap bitmap1;

    public MyAdapter(List list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHoder hoder;
        if (view==null){
            view= View.inflate(context, R.layout.itme_layout, null);
            hoder=new ViewHoder();
            hoder.img=(ImageView)view.findViewById(R.id.img);
            hoder.text=(TextView)view.findViewById(R.id.text);
            view.setTag(hoder);
        }else {
            hoder= (ViewHoder) view.getTag();
        }

        hoder.text.setText(list.get(i).getTitle());
        new MyTask(hoder.img).execute("http:"+list.get(i).getImage_url());······················
        return view;
    }
    class MyTask extends AsyncTask {》》》》》》》》》》》》》》》》》》》》》》》》》》
private ImageView img;

        public MyTask(ImageView img) {
            this.img = img;
        }

        //运行在后台,进行网络请求,子线程
        @Override
        protected Bitmap doInBackground(String... strings) {//...可变长类型 string....相当于 String[]
      Bitmap bitmap=null;
            try {
                //1.创建URL
                URL url = new URL(strings[0]);
                //2.打开连接
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                //3.设置
                urlConnection.setRequestMethod("GET");
                urlConnection.setReadTimeout(5000);
                urlConnection.setConnectTimeout(5000);
                //4.判断响应码
                if (urlConnection.getResponseCode() == 200) {
                    //5.获取数据
                    InputStream inputStream = urlConnection.getInputStream();
                    bitmap1 = BitmapFactory.decodeStream(inputStream);



                }


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bitmap1;
        }

        @Override
        protected void onPostExecute(Bitmap s) {
img.setImageBitmap(s);

        }
    }》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》




    public class ViewHoder{
        private ImageView img;
        private TextView text;
    }
}

你可能感兴趣的:(网络请求的图片,在Adapter(适配器)中异步的处理)