简体   繁体   中英

displaying images in a listview

I'm working on an application that shows a list of images (from a URL) and some text in a ListView. It looks like that (+'s are images):

-------------------------
|   +++       some text |  
|   +++       some text |  
-------------------------
|   +++       some text |  
|   +++       some text |  
-------------------------
|   +++       some text |  
|   +++       some text |  
-------------------------
|   +++       some text |  
|   +++       some text |  
-------------------------

I'm getting the URLs and the text by parsing a string that contains the source code of a certain web page, and I'm getting that source string using a HttpGet request.

So this application needs to download a web page, parse it and then download some images and display it in a list.

I'm not sure about the last part. I managed to download and parse html source code using asynctask, but the rest is a bit tricky for me.

How can I do that?

(I hope this was clear enough)

Edit:

I'll try to be more accurate now :) I've created an object called MediaObject, which has 3 strings (image URL, song name and song's artist). Iv'e also written a function that gets a string (that holds an html source code), extracts from it the URLs and the names and returns an array of MediaObject objects.

At this moment I display only 1 song, using 2 asynctasks: 1) To get the html source code, then get a MediaObject object and display the song/artist name in a textview. 2) Download the image and display it in a imageview.

It works fine, but only for 1 MediaObject. I want to display in a listview several songs (using several MediaObject objects).

The problem is that I'm not sure how to do that. Should I use several asynctasks? new asynctask for every image?

To start with, break it up into pieces. Dont think of it all together. I recommend, the following.

  • Write a junit test case, do make the http call, parsing and extracting the image link.
  • Now, create a new android junit test case, get the above piece of code into a async task, create a layout with just 1 image, and render the image on it.
  • Now, you are ready to integrate it into your app.

The idea of the junit is, that you will be able to write this piece of code and test it in quick successions, since you are writing it for the first time. Keep in mind that speed of development is very important.

I found a interesting example of async task here .

As for your question, you need to be clearer on the kind of http call you are making and the kind of return it provides. Json, XML, what ?

check the link . here there is a function named showImage() which accepts a url as parameter and returns bitmap which you can use to set to the image view. And for the custom list view for displaying the image you need to customize the list as in here and set the image using something like imageview1.setImageBitmap(bm);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM