简体   繁体   中英

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!

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.

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 eg this TweetAdapter that uses this layout The left screenshot shows how this looks like.

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