2011-06-10 8 views
5

Quiero mostrar noticias (desde RSS) en un ListView. Ya logré crear mi ListView, mostrar correctamente tanto el título como la descripción. Pero no puedo mostrar una imagen desde la URL.Mostrar imagen dibujable de url en un ListView

Aquí está mi código:

maListViewPerso = (ListView) findViewById(R.id.listviewperso); 
ArrayList <HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>(); 
HashMap<String, String> map; 

int i = 0; 
while (i < 55) 
{ 
    map = new HashMap<String, String>(); 
    map.put("titre", newsArray[i]); 
    map.put("description", newsArray[i+1])); 
    map.put("img", newsArray[i+4]); 
    listItem.add(map); 

    i += 5; 
} 

SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, 
R.layout.affichageitem, new String[] {"img", "titre", "description"}, 
new int[] {R.id.img, R.id.titre, R.id.description}); 

maListViewPerso.setAdapter(mSchedule); 

Como se puede adivinar, newsArray[i+4] contiene la URL de mi imagen.

He escrito una función drawable_from_url que devuelve un android.graphics.drawable.Drawable y que funciona bien. Traté de escribir esto:

map.put("img", String.valueOf(drawable_from_url(newsArray[i+4], "newsPic"))); 

Pero tampoco funciona (no se muestra ninguna imagen). Un ejemplo del valor de String.valueOf(drawable_from_url(newsArray[i+4], "newsPic"))
podría ser
[email protected]

Alguna idea?

Gracias.

Respuesta

0

probar este método para cargar mapa de bits de url

public Bitmap setRemoteImage(URL url) { 
    try { 
     Bitmap bm=null; 
     HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 
     conn.setDoInput(true); 
     conn.connect(); 
     InputStream is = conn.getInputStream(); 
     BufferedInputStream bis=new BufferedInputStream(is); 
     try { 
      bm = BitmapFactory.decodeStream(bis); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      MainActivity mm= new MainActivity(); 
      Drawable dra=mm.getResources().getDrawable(R.drawable.icon); 
      Bitmap bb=((BitmapDrawable)dra).getBitmap(); 
      bm=bb; 
     } 
     is.close(); 
     return bm; 
    }catch (IOException e) { 
     return null; 
    } 
}