1

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 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.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
Php Pete
  • 762
  • 3
  • 14
  • 29

2 Answers2

0

Using setImageResource().

imageView.setImageResource(R.drawable.some_image);
Robby Pond
  • 73,164
  • 16
  • 126
  • 119
  • Hi Robby thanks for the help.I was going to ask how to use this in my approach but have realised I need to create a custom adapter and use the method suggested.Thanks again! – Php Pete Mar 21 '11 at 17:04
0

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.

Community
  • 1
  • 1
  • Here it is: http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html It uses built-in downloader instead of custom(like in Fedor's LazyList) –  Mar 21 '11 at 17:03
  • That's for dynamic image loding from remote server, as for pre-stored images you'll also need to create adapter. –  Mar 21 '11 at 17:06