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.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
/**
* Created by lenovo on 2017/12/2.
*/
class MyAdapter extends BaseAdapter {
private List<Result.NewslistBean>list;
private Context context;
public MyAdapter(List<Result.NewslistBean> 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) {
view = View.inflate(context, R.layout.item, null);
TextView textview = (TextView) view.findViewById(R.id.title);
TextView text2 = (TextView) view.findViewById(R.id.text2);
ImageView imageView =(ImageView) view.findViewById(R.id.img);
textview.setText(list.get(i).getTitle());
text2.setText(list.get(i).getCtime());
new MyTask(imageView).execute(list.get(i).getPicUrl());
return view;
}
class MyTask extends AsyncTask<String,Void,Bitmap> {
private ImageView img;
public MyTask(ImageView img) {
this.img = img;
}
@Override
protected Bitmap doInBackground(String... strings) {
Bitmap bitmap = null;
StringBuilder builder = new StringBuilder();
try {
URL url = new URL(strings[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
if (connection.getResponseCode() ==200){
InputStream inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
}
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap s) {
img.setImageBitmap(s);
}
}
}