Dev1345
Dev1345

Reputation: 61

How to display actual image based on file name in sqlite database

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!

Upvotes: 0

Views: 1755

Answers (2)

Moog
Moog

Reputation: 10193

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:

void populateFrom(Cursor c, ColorHelper r) {

    //get the color name from your database (only once)

    String strColor = r.getName(c);

    // set the text on your TextView as before

    TextView clrName = (TextView)findViewById(R.id.ImageView00);
    clrName.setText(strColor);

    // get the resource ID - note "name" without extension

    int resourceID = context.getResources().getIdentifier(strColor.toLower(),
            "drawable", context.getPackageName());

    //get a reference to your imageview and set the image

    ImageView clrImage = (ImageView)findViewById(R.id.ImageView00);
    clrImage.setImageResource(resourceID);

}

Upvotes: 2

Lazy Ninja
Lazy Ninja

Reputation: 22527

There are several ways to do it. If I had to do it myself I would save the images as Blob in the database. Straight forward and easy to use. The other alternative is to use if - else if - else if like this:

ImageView imageview = findViewById(R.id.ImageView00);
    if( colorname.equals("Red") )
        imageview.setImageResource(R.drawable.red);
    else if( colorname.equals("Red") )
        imageview.setImageResource(R.drawable.blue);

Upvotes: 1

Related Questions