在Android的ImageView中动态加载图像

问题描述:

我有我的可绘制文件夹中的(国旗)100 +图像的数据库。在Android的ImageView中动态加载图像

现在我想在ImageView中显示当前所在国家/地区的国旗。

我拿到国内用String country_variable = address.getCountryCode();

我设定flag.setImageDrawable(getResources().getDrawable(R.drawable.country_variable));

像大家都知道R.drawable.country_variable wont't工作,因为编译器不能找到一个名为country_variable图像可绘制的文件夹。

这样做的最好方法是什么?

+0

为什么你不用每个drawable的国家代码来命名?并使用散列表。 – techiServices 2010-11-23 13:20:30

+0

我做到了。这些标志被命名为“fr.png”,“de.png”,“uk.png”等。 – Galip 2010-11-23 13:24:26

+0

没有解释我自己对不起。 hashmap 其中int是R.drawable。 techiServices 2010-11-23 13:30:09

您应该能够使用getResources()则getIdentifier()由资源名称来获得ID。例如:

flag.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("drawable/" + country_variable, "drawable", getPackageName())); 

试试这个:

flag.setImageDrawable(getResources().getDrawable(getResources().getIdentifier(country_variable, "drawable", getPackageName()));