Chargement Asynchrone d'image dans une liste
Bonjour,
Voici donc le dernier gros problème qui me tarode depuis quelques jours :
Voici l'adaptateur de ma liste comme il fonctionne actuellement. Il fonctionne bien MAIS il ralentit considérablement le défilement de ma liste.
Ce que je souhaite c'est donc faire un chargement asynchrone ou via AsyncTask ou un thread basique. J'ai déjà regarder beaucoup de tutaux et autres sujet de forum mais je ne suis arrivé à rien de concluant. :(
C'est pourquoi je vous sollicite de nouveau !
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
public class AdaptateurListeNews extends BaseAdapter {
private ArrayList<String> images;
private ArrayList<String> titres;
private LayoutInflater monInflater;
public AdaptateurListeNews (Context context, ArrayList<String> _titres, ArrayList<String> _images){
this.monInflater = LayoutInflater.from(context);
this.images = _images;
this.titres = _titres;
}
@Override
public int getCount() {
return this.titres.size();
}
@Override
public Object getItem(int position) {
return this.titres.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
ImageView icon;
TextView text;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = monInflater.inflate(R.layout.liste_news, null);
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.iconnews);
holder.text = (TextView) convertView.findViewById(R.id.textnews);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(titres.get(position));
holder.icon.setImageDrawable(loadImageFromUrl(images.get(position)));
return convertView;
}
public static Drawable loadImageFromUrl(String url) {
InputStream inputStream;
try {
inputStream = new URL(url).openStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
return Drawable.createFromStream(inputStream, "src");
}
} |
Merci d'avance !