0

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,
        R.id.totalSongs};

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);
albumArt.setImageURI(currentSongUri);

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.

--Edit--

Two great answers. Thank you both.

2 Answers2

0

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/

user936414
  • 7,574
  • 3
  • 30
  • 29
  • Thanks for your answer. Could you elaborate on the "constructor" bit? I don't know which part of that example is the constructor.. –  Jun 04 '12 at 06: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 04 '12 at 06:41
  • Thanks. I'll have to have a play with this, my understanding is quite limited at the moment. –  Jun 04 '12 at 06:44
0
private class YourCustomAdatper extends CursorAdapter{

private final LayoutInflater mInflater;

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

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

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view =mInflater.inflate(yourLayout, parent, false);

return view;
    }

}
Zaz Gmy
  • 4,236
  • 3
  • 19
  • 30
  • 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? –  Jun 04 '12 at 06:36
  • modify your layout. and getviews in bindView. – Zaz Gmy Jun 04 '12 at 06:38