开发者

Android - store/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);
setListAdapte开发者_如何学编程r(activities);

On the second line, I'm grabbing the name, data, and thumb from the database. On the third line, I'm setting those values for activityName (TextView), activityData (TextView), and activityThumb (ImageView).

It works fine for "name" and "data", but I have no idea what to put for "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 save it as raw data somehow?)

Best case scenario: I'd like to just tell each instance of activityThumb in the list what image path it should be using as its resource.

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


R.drawable.IMAGE_NAME is an integer. You can store it into the database, but it might change on the next build and break all existing databases.

You can do an enum with 2 fields: One "external representation" string that you can store in the database and one int that represents the resource identifier. You would also need a way to restore the enum from the string representation, e.g. using a map.


I figured it 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜