MyAdapter 适配器

package com.example.myzhoukao_one.Activity;

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 com.example.myzhoukao_one.R;

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

/**
 * Created by Administrator on 2017/10/10.
 */

public class MyAdapter extends BaseAdapter{


    Context context;
    List list;

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

    @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) {

        ViewHolder holder;
        if(view == null){
           view = View.inflate(context, R.layout.item_layout,null);

            holder = new ViewHolder();

            holder.iv = view.findViewById(R.id.image_view);
            holder.textView = view.findViewById(R.id.text_view);

            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }

        displayImage(list.get(i).getPicUrl(),holder.iv);
        holder.textView.setText(list.get(i).getTitle());

        return view;
    }
    private void displayImage(final String picUrl, final ImageView iv) {
        //异步处理
        AsyncTask< Void,Void,Bitmap> asyncTask = new AsyncTask< Void, Void, Bitmap>() {

            @Override
            protected Bitmap doInBackground(Void... voids) {
                //得到图片的网络格式
                try {
                    URL url = new URL(picUrl);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    //设置
                    connection.setRequestMethod("GET");
                    connection.setReadTimeout(5000);
                    connection.setConnectTimeout(5000);
                    //获取
                    int responseCode = connection.getResponseCode();
                    if(responseCode == 200){
                        InputStream inputStream = connection.getInputStream();
                        //转成Bitmap
                        Bitmap map = BitmapFactory.decodeStream(inputStream);
                        return map;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                iv.setImageBitmap(bitmap);
            }
        };
        asyncTask.execute();
    }

    private class ViewHolder{
        ImageView iv;
        TextView textView;
    }
}

你可能感兴趣的:(MyAdapter 适配器)