Take the 2-minute tour ×

I'm trying to populate a listview with images, whose URI is returned from a cursor.

I'm not sure if I should use a viewbinder with my simplecursoradapter, or to create a custom simplecursoradapter that somehow does the same job, and I also don't know how to implement either of those options.

My adapter has the following:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
        R.layout.albumitem, albumCursor, displayFields, displayViews);

String[] displayFields = new String[] { AudioColumns.ALBUM,
        AudioColumns.ARTIST, AlbumColumns.NUMBER_OF_SONGS };

int[] displayViews = new int[] { R.id.albumTitle, R.id.artistTitle,

But I'd like to add an image to R.id.albumView as well.

I can obtain the image normally (outside of an adapter) by retrieving the AlbumColumns.ALBUM_ID from the cursor, and then using the following code:

currentAlbumId = idList.get(currentSongIndex);
currentAlbumIdLong = Integer.parseInt(currentAlbumId);
artworkUri = Uri.parse("content://media/external/audio/albumart");
currentSongUri = ContentUris.withAppendedId(artworkUri, currentAlbumIdLong);

My problem is, I have no idea how to perform a similar task inside the adapter. Anyhow, my best guess is to use a viewBinder. Could somebody kindly show me how to implement this?

Thank you for your help.


Two great answers. Thank you both.

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted
private class YourCustomAdatper extends CursorAdapter{

private final LayoutInflater mInflater;

public YourCustomAdatper(Context context, Cursor cursor) {
super(context, cursor, true);
mInflater = LayoutInflater.from(context);

public void bindView(View view, Context context, final Cursor cursor)
ImageView imageview=view.findViewById(yourImageview_id);
//do rest of task

final View view =mInflater.inflate(yourLayout, parent, false);

return view;

share|improve this answer
Thankyou Zaz Gmy. Do I need to modify any of that to include the textviews, or does this adapter deal with them by virtue of extending the cursoradapter? –  user1352742 Jun 4 '12 at 6:36
modify your layout. and getviews in bindView. –  Zaz Gmy Jun 4 '12 at 6:38
add comment

You can use a custom CursorAdpater. Pass the arguments you need to the constructor of the Custom class. In the newView inflate the layout and in bindView set the values. Refer http://blog.cluepusher.dk/2009/11/16/creating-a-custom-cursoradapter-for-android/

share|improve this answer
Thanks for your answer. Could you elaborate on the "constructor" bit? I don't know which part of that example is the constructor.. –  user1352742 Jun 4 '12 at 6:33
Instead of SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.albumitem, albumCursor, displayFields, displayViews); give CustomCursorAdpater adapter = new CustomCursorAdapter(this,albumCursor, displayFields, displayViews); –  user936414 Jun 4 '12 at 6:41
Thanks. I'll have to have a play with this, my understanding is quite limited at the moment. –  user1352742 Jun 4 '12 at 6:44
add comment

Your Answer

