Using setImageDrawable dynamically to set image in an ImageView

前端 未结 17 1826
借酒劲吻你
借酒劲吻你 2020-12-04 08:22

I am generating a string from database dynamically which has the same name of image in drawable folder.

Now I want to set that value for ImageView

17条回答
  •  忘掉有多难
    2020-12-04 08:50

    Try this,

    int id = getResources().getIdentifier("yourpackagename:drawable/" + StringGenerated, null, null);
    

    This will return the id of the drawable you want to access... then you can set the image in the imageview by doing the following

    imageview.setImageResource(id);
    

提交回复
热议问题