Advertisement
Guest User

Untitled

a guest
May 1st, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. @Override
  2. public View getView(int position, View convertView, ViewGroup parent) {
  3. LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  4. if(convertView==null)
  5. {
  6. convertView = inflater.inflate(R.layout.news_grid_item,null);
  7. }
  8. ArticleModel am = getItem(position);
  9.  
  10. TextView cat = (TextView)convertView.findViewById(R.id.cat);
  11.  
  12. cat.setText(am.getCategorie().name());
  13. TextView title = (TextView)convertView.findViewById(R.id.title);
  14. title.setText(am.getTitle());
  15.  
  16. TextView date = (TextView)convertView.findViewById(R.id.date);
  17. date.setText(am.getDate());
  18. Log.i("Adding news ", am.toString());
  19.  
  20. String mediaUrl = am.getMediaURL();
  21. if(am.getMediaType().equals(MediaType.vidéo))
  22. {
  23. String videoId = mediaUrl.split("=")[1];
  24. mediaUrl = "https://img.youtube.com/vi/"+videoId+"/default.jpg";
  25. }
  26. final ImageView apercu = (ImageView)convertView.findViewById(R.id.apercu);
  27. new AsyncTask<String, Void, Bitmap>() {
  28.  
  29. @Override
  30. protected Bitmap doInBackground(String... params) {
  31. return getBitmapFromURL(params[0]);
  32. }
  33.  
  34. @Override
  35. protected void onPostExecute(Bitmap bitmap) {
  36. apercu.setImageBitmap(bitmap);
  37. }
  38. }.execute(mediaUrl);
  39.  
  40. return convertView;
  41. }
  42.  
  43. public Bitmap getBitmapFromURL(String src) {
  44. try {
  45. java.net.URL url = new java.net.URL(src);
  46. HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  47. connection.setDoInput(true);
  48. connection.connect();
  49. InputStream input = connection.getInputStream();
  50. Bitmap myBitmap = BitmapFactory.decodeStream(input);
  51. return myBitmap;
  52. } catch (IOException e) {
  53. e.printStackTrace();
  54. return null;
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement