Android ImageView填充动态值

Android ImageView填充动态值,android,dynamic,imageview,Android,Dynamic,Imageview,对安卓来说还是新鲜事。温柔点。我读过很多关于动态填充ImageView的文章。我需要以某种方式来做,但我正在努力使用正确的语法。具有讽刺意味的是,我知道如何在iOS上做到这一点,但我完全被Android难倒了。下面是代码 我想动态填充图像资源的名称及其位置 //Set the string to lower case String animalImageLocation = animalName.toLowerCase()+"icon"; //Prepare to set the value o

对安卓来说还是新鲜事。温柔点。我读过很多关于动态填充ImageView的文章。我需要以某种方式来做,但我正在努力使用正确的语法。具有讽刺意味的是,我知道如何在iOS上做到这一点,但我完全被Android难倒了。下面是代码

我想动态填充图像资源的名称及其位置

//Set the string to lower case
String animalImageLocation = animalName.toLowerCase()+"icon";
//Prepare to set the value of the ImageView
animalView = (ImageView)findViewById(R.id.animalView);
//Dynamically populate the image into the animalView
animalView.setImageResource(getResources().getIdentifier("R.Drawable."+<dynamicValue??>,null,null));
我尝试过几种方法,它们要么不显示图像,要么干脆崩溃。我需要使用图像名称有效地填充getIdentifier(),即R.Drawable.bearicon

有什么想法吗


杰里米

首先,
R.drawable
没有资本,我认为这很重要。另外,如果您将
null
作为类传递,那么您也必须提供该类

请尝试以下操作:

animalView.setImageResource(
    getResources().getIdentifier(
        animalImageLocation,
        "drawable",
        animalView.getContext().getPackageName()
    )
);
或者,如果您希望为后一个参数传递
null
,则可能是这样:

animalView.setImageResource(
    getResources().getIdentifier(
        animalView.getContext().getPackageName() + ":drawable/" + animalImageLocation,
        null,
        null
    )
);

给出一些你想动态显示的图片的例子。你想在绘图表中得到的文件名是什么?谢谢。知道了。我以为.getPackageName()正在获取图像的名称。我把它倒过来了!再次感谢!
animalView.setImageResource(
    getResources().getIdentifier(
        animalView.getContext().getPackageName() + ":drawable/" + animalImageLocation,
        null,
        null
    )
);