作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想从存储在 JSON 文件中的链接向我的 iamgeview 添加图像,如下所示:
{
"parts":[
{"name": "Bosch Iridium",
...
...
...
"image": "R.drawable-hdpi.plug_boschi"
},
现在我 zipper 接并用这段代码显示它:
try {
jObject = new JSONObject(sJSON.substring(sJSON.indexOf('{')));
JSONArray pluginfo = jObject.getJSONArray("parts");
JSONObject e = pluginfo.getJSONObject(position);
String imagefile = e.getString("image");
Drawable image = getDrawable(imagefile);
ImageView itemImage = (ImageView) findViewById(R.id.item_image);
itemImage.setImageDrawable(image);
} catch (JSONException e) {
e.printStackTrace();
}
}
我很确定这部分是正确的
ImageView itemImage = (ImageView) findViewById(R.id.item_image);
itemImage.setImageDrawable(image);
但我需要有关上面从 JSON 数组获取链接的部分的帮助,以便我可以显示它。
最佳答案
您需要先从 JSON 中包含的字符串中获取资源 ID。
String imagefile = e.getString("image");
String resName = imagefile.split("\\.")[2]; // remove the 'R.drawable.' prefix
int resId = getResources().getIdentifier(resName, "drawable", getPackageName());
Drawable image = getResources().getDrawable(resId);
关于android - 如何从 JSON 数组动态地将图像添加到 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13455908/
我是一名优秀的程序员,十分优秀!