Save / set imageResource path in database?

Cursor c = dbHelper.getMostRecent();
String[] from = new String[] { "name", "data", "thumb" };
int[] to = new int[] { R.id.activityName, R.id.activityData, R.id.activityThumb };

startManagingCursor(c);
SimpleCursorAdapter activities;
activities = new SimpleCursorAdapter(this, R.layout.activitywidget, c, from, to);
setListAdapter(activities);

In the second line, I grab the name, data, and thumb from the database. In the third line, I set these values ​​for activityName (TextView), ActivityData (TextView) and ActivityThumb (ImageView).

It works great for "name" and "data", but I have no idea what to put for the "thumb". I have some images in / res / drawable /, but I'm not sure how to store them in the database (file path as a string? R.drawable.IMAGE_NAME as a string? Do I need to somehow save it as raw data ?)

Best scenario: I would just like to tell each activityThumb instance in the list which image path it should use as its resource.

EDIT: I figured this out by creating an implementation of SimpleCursorAdaptor and overriding bindView.

+3
source share
2 answers

R.drawable.IMAGE_NAME is an integer. You can save it to the database, but it may change the next time you build it and break all existing databases.

You can do an enumeration with two fields: one line of the "external view" that you can save in the database, and one int that represents the resource identifier. You will also need a way to restore an enumeration from a string representation, for example. using the card.

0
source

I figured this out by creating an implementation of SimpleCursorAdaptor and overriding bindView.

public void bindView(View view, Context context, Cursor cursor) {
    ImageView imageView = (ImageView) view.findViewById(R.id.image);

    //do stuff with image here

    super.bindView(view, context, cursor);

}

0
source

All Articles