Populating android list with images
A background before I start, I am a beginner in android and have looked on various forums for a solution to my problem but found the overly complicated for what I am trying to achieve, so I am here to ask the good people at stack!
As the title says I am currently trying to populate a list with custom images, one image for each row on the list.
The .xml file for a row has the following container
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
and开发者_JAVA百科 I understand the image is retrieved from :src.
What I am trying to achieve is to have the res/drawable folder storing my images.I would then like to add the appropriate image to its corresponding list, and this is where I run into a problem;
How can I replace the android:src value dynamically from inside my java code with the required image.
Using setImageResource().
imageView.setImageResource(R.drawable.some_image);
Take a look at Lazy load of images in ListView that's one of the best solutions. You'll certanly find it useful. As for algorithm: to populate list you need an Adapter, supplied to it. You'll have to create custom adapter. In the adapter you are to implement getView() method. There youl create imageview and populate it with your image. There is a nice tutorial in Android developers blog. The other important thing is View recycling. It allows you not to create new view every time you need to populate list item. Don't forget about it.
精彩评论