List View with Textview and Imageview

Asked

Viewed 169 times

2

I’m trying to make a ListView with TextView and ImageView(obtained through a URL), when only with TextView was appearing normal, but when I added the code to show the images, the screen turns all white. What could be the problem?
I used the method setImageURI() to set the image link. the code I’m using is as follows::

  public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;
    StockQuoteView sqView = null;

    if(rowView == null)
    {
        // Get a new instance of the row layout view
        LayoutInflater inflater = activity.getLayoutInflater();
        rowView = inflater.inflate(R.layout.noticia_list_item, null);

        // Hold the view objects in an object,
        // so they don't need to be re-fetched
        sqView = new StockQuoteView();
         //sqView.quote = (TextView) rowView.findViewById(R.id.ticker_symbol);
        sqView.ticker = (TextView) rowView.findViewById(R.id.ticker_price);
        sqView.img  = (ImageView) rowView.findViewById(R.id.img);     
        // Cache the view objects in the tag,
        // so they can be re-accessed later
        rowView.setTag(sqView);
    } else {
        sqView = (StockQuoteView) rowView.getTag();
    }

    // Transfer the stock data from the data object
    // to the view objects
    Noticias currentStock = (Noticias) stocks.get(position);
    sqView.ticker.setText(currentStock.getTitulo());
    sqView.img.setImageURI(currentStock.getImagem());// bom aqui eu pensei que o setImageURI iria setar o link da imagem e assim exibi-la... mas é necessário uma conversão pra bitmap?

  // sqView.quote.setText(currentStock.getDescricao());

    return rowView;
  • 3

    Edit the question and add the code you’re using that might make it easier to help you.

1 answer

2


Do not confuse URL with URI. You cannot request data from a URL, example: www.meusite.com.br/imagens/imagem.png, through setImageURI().

This is one thing [searches the image on your device through the path given in the URI]:

Uri imgUri=Uri.parse("file:///data/data/MINHA_PASTA_ANDROID/minhaimage.png");
imgview.setImageUri(imgUri);

And this is something else [Convert the Bitmap image of a URL]:

private Bitmap getImageBitmap(String url) {
    Bitmap bm = null;
    try {
        URL aURL = new URL(url);
        URLConnection conn = aURL.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        bm = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();
   } catch (IOException e) {
       Log.e(TAG, "Error getting bitmap", e);
   }
   return bm;
} 
  • Thank you very much...

  • Did it help? Vote positive!

  • I believe you meant URL and not ULR, it’s not?

  • I did not understand very well, I have the image link and I was trying to set in img,(using the setImageURI() to display the image... is this wrong? I need to convert the link to bitmap to display it?

  • Exactly. You need to convert the image to Bitmap. Ask another question and I’ll answer you how :)

  • That’s right, @Matthew.

  • Well if that’s it, thank you very much Rafael, it was very helpful... :)

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.