gpt4 book ai didi

android - 如何从 JSON 数组动态地将图像添加到 ImageView

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:54:33 32 4
gpt4 key购买 nike

我想从存储在 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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com