smotru
smotru

Reputation: 431

Change ImageView dynamically knowing file name from database

In my application I am getting the filename dynamically from database but I'm not sure how to update the image of ImageView.

Let's say I have this code:

String str = "image2.jpg";
ImageView imageView = (ImageView) findViewById(R.id.image);

How could I set the image to be image2.jpg, if current image is another?

PS. In my application, I have a spinner. Every time the selected element is changed, I make a database request and I get the image name, and I need to modify image of imageview dynamically.

Upvotes: 0

Views: 1195

Answers (1)

kalyan pvs
kalyan pvs

Reputation: 14590

write a method like this in your Activity

public Drawable getImage(String name) {
    return getResources().getDrawable(
            getResources().getIdentifier(name, "drawable",
                    getPackageName()));
}

pass you image name here you will get drawable object then set it to Imageview

    Drawable image = getImage(name);
    imageView.setImageDrawable(image);

Upvotes: 1

Related Questions