Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- if(convertView==null)
- {
- convertView = inflater.inflate(R.layout.news_grid_item,null);
- }
- ArticleModel am = getItem(position);
- TextView cat = (TextView)convertView.findViewById(R.id.cat);
- cat.setText(am.getCategorie().name());
- TextView title = (TextView)convertView.findViewById(R.id.title);
- title.setText(am.getTitle());
- TextView date = (TextView)convertView.findViewById(R.id.date);
- date.setText(am.getDate());
- Log.i("Adding news ", am.toString());
- String mediaUrl = am.getMediaURL();
- if(am.getMediaType().equals(MediaType.vidéo))
- {
- String videoId = mediaUrl.split("=")[1];
- mediaUrl = "https://img.youtube.com/vi/"+videoId+"/default.jpg";
- }
- final ImageView apercu = (ImageView)convertView.findViewById(R.id.apercu);
- new AsyncTask<String, Void, Bitmap>() {
- @Override
- protected Bitmap doInBackground(String... params) {
- return getBitmapFromURL(params[0]);
- }
- @Override
- protected void onPostExecute(Bitmap bitmap) {
- apercu.setImageBitmap(bitmap);
- }
- }.execute(mediaUrl);
- return convertView;
- }
- public Bitmap getBitmapFromURL(String src) {
- try {
- java.net.URL url = new java.net.URL(src);
- HttpURLConnection connection = (HttpURLConnection)url.openConnection();
- connection.setDoInput(true);
- connection.connect();
- InputStream input = connection.getInputStream();
- Bitmap myBitmap = BitmapFactory.decodeStream(input);
- return myBitmap;
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement