I have a local sqlite database file that looks something like this…
_ID | Color_Name | Image_Name
1 | Red | red.png
2 | Blue | blue.png
3 | Green | green.png
Etc.
The Image_Name column of the database contains ONLY the file name, NOT the image itself. All images are stored locally in the res/drawable-mdpi directory.
The goal is to have a list that displays the color name and the associated image for that color from the database. For example, the list would look something like this [the plus sign below represents an actual .png image]…
____________________________________________
Red (text only on this line)
+ (actual red.png image on this line)
____________________________________________
Blue (text only on this line)
+ (actual blue.png image on this line)
____________________________________________
Etc.
I can get the color name to display, but I can’t figure out how to get the image itself to display. The relevant portion of code for displaying the name is below…
static class ColorHolder {
private TextView name=null;
ColorHolder(View row) {
name=(TextView)row.findViewById(R.id.colorName);
}
void populateFrom(Cursor c, ColortHelper r) {
name.setText(r.getName(c)) ;
}
}
The relevant portion of the xml file for BOTH the name & image is below…
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|left"
android:id="@+id/colorName"/>
<ImageView
android:id="@+id/ImageView00"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/image1" <!--[NOTE: THIS DEFAULT IMAGE FILE SHOULD BE REPLACED ON THE FLY W/THE CORRECT IMAGE FILE FOR EACH RECORD]-->
>
None of the other questions I found were helpful in resolving this issue for me. Could someone please help me get this figured out? I would be happy for any help, but it would be especially helpful if you could use my ACTUAL file names / paths / variables / ID’s / etc. (which are given above) in your explanation so that I could most easily follow & understand your response. Pointing me to tutorials may not be helpful as I have already looked at a lot of material but didn’t get this resolved. Thanks!
Use Resources.getIdentifier(String name, String defType, String defPackage)
Also you don’t need to store the resource image name … just name them the same as the color (as you already have done), just make sure as I show here that they are lower case
To use your example: