Anand Sainath
Anand Sainath

Reputation: 1807

Image in a List view in Android

Hey all, I know that this question has been asked many times in this forum itself. But I am not able to get a working solution out of any one of them.

My problem is that I need to create a list view with images. The content is static. Eg: Lets say i need to display 5 rows of data and I have 5 images to go with these rows. All i need to do is to display them in a list format!

Simple right??

Thanks in advance!

Upvotes: 0

Views: 637

Answers (2)

Harshad
Harshad

Reputation: 8014

Hey nice to see somebody facing the same problem as I did. Here is the very simple solution for that. Make a ArrayList add your five elements in that named COUNTRIES. create one list_item.xml file with one ImageView named imageview and one TextView named textview in a linear layout(This layout will repeat for your each row in ListView). Dont forget to set image in ImageView to any defult icon or something.

in your class that extends ListActivity simply enter following line and you are done.

setListAdapter(new ArrayAdapter<String>(getApplicationContext(),R.layout.list_item,R.id.textview,COUNTRIES));

In case you stuck anywhere you can ask.

Upvotes: 2

Heiko Rupp
Heiko Rupp

Reputation: 30934

You need to write a custom layout for the rows and pass it to the Adapter that defines the content of the list (you most probably want to write your own, but there may be some predefined ones).

See e.g. this TweetAdapter that uses this layout The left screenshot shows how this looks like.

Upvotes: 0

Related Questions