字符串值到varaiable [英] String value to a varaiable

查看:256
本文介绍了字符串值到varaiable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个关于在Android的Java问题 - 日食
我有一个字符串研究
为了记录在案,我defind他这样的:

this is a question about java on android - eclipse I have a String r for the record, I defind him like that:

String r="pic"+c1;

我想利用它的价值,是至极pic6,并把它作为一个varaiable名称。

I want to take the value of it, wich is "pic6" and use it as a varaiable name.

我该怎么办呢?

推荐答案

如果你想选择一个图像名称为pic6.png从Android应用程序的绘制文件夹,您可以尝试以下code。

If you want to pick an image with name "pic6.png" from the drawable folder of the Android application, you can try the following code.

String r = "pic" + c1;

int resId = getResources().getIdentifier(r, "drawable", getPackageName());
if (resId != 0) {
    imageView.setImageResource(resId);
} else {
    // This is used in case no image resource is found
    imageView.setImageResource(R.drawable.unknown);
}

请参阅:如果您使用其它的部分,那么你需要添加unknown.png形象到Android项目的绘制文件夹

Please see: If you use the else part, then you need to add "unknown.png" image to the drawable folder of the Android project.

希望这有助于。

这篇关于字符串值到varaiable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆