我无法在我的应用程序中使用JSON显示来自drawable的图像。

huangapple 未分类评论45阅读模式
英文:

i cant show image from drawable in my app using JSON

问题

我已经正确地从资源文件(recipe.json)中读取了我的 JSON 数据,该数据包含:

  1. {
  2. "id":"1",
  3. "name":"الوصفة 1",
  4. "desc":"الوصفة 1",
  5. "image":"R.drawable.image"
  6. }

我在 RecyclerView 中正确地加载了名称和描述,但是使用 getIdentifier() 方法无法显示图像...这是我的代码:

  1. private void addItemsFromJSON() {
  2. try {
  3. String jsonDataString = readJSONDataFromFile();
  4. JSONObject jsonObject = new JSONObject(jsonDataString);
  5. JSONArray jsonArray = jsonObject.getJSONArray("recipes");
  6. for (int i = 0; i < jsonArray.length(); ++i) {
  7. JSONObject itemObj = jsonArray.getJSONObject(i);
  8. String name = itemObj.getString("name");
  9. String desc = itemObj.getString("desc");
  10. String image = itemObj.getString("image");
  11. // 通过图像名称获取资源ID
  12. int resourceId = getResources().getIdentifier(image, "drawable", getPackageName());
  13. Recipe recipe = new Recipe(name, desc, resourceId);
  14. recipes.add(recipe);
  15. }
  16. } catch (JSONException | IOException e) {
  17. Log.d(TAG, "addItemsFromJSON: ", e);
  18. }
  19. }

它会显示标题和描述,但图像为空。

英文:

i did everything right to read my json data from assets (recipe.json) which contains

{"id":"1",
"name":"الوصفة 1",
"desc":"الوصفة 1",
"image":"R.drawable.image"}...

i loaded name and desc correctly in recyclerView but the image doesnt show using getIdentifier() ... this is my code

  1. private void addItemsFromJSON() {
  2. try {
  3. String jsonDataString = readJSONDataFromFile();
  4. JSONObject jsonObject = new JSONObject(jsonDataString);
  5. JSONArray jsonArray=jsonObject.getJSONArray(&quot;recipes&quot;);
  6. for (int i=0; i&lt;jsonArray.length(); ++i) {
  7. JSONObject itemObj = jsonArray.getJSONObject(i);
  8. String name = itemObj.getString(&quot;name&quot;);
  9. String desc = itemObj.getString(&quot;desc&quot;);
  10. String image = itemObj.getString(&quot;image&quot;);
  11. // get resource id by image name
  12. int resourceId =getResources().getIdentifier(image, &quot;drawable&quot;,getPackageName());
  13. Recipe recipe = new Recipe(name, desc,resourceId);
  14. recipes.add(recipe);
  15. }
  16. } catch (JSONException | IOException e) {
  17. Log.d(TAG, &quot;addItemsFromJSON: &quot;, e);
  18. }
  19. }

it shows the title and describtion but image is empty

答案1

得分: 0

Modify image name in the JSON like

  1. {"id":"1", "name":"image1", "desc":"Image Description", "image":"imageNameInTheDrawable"}

Get the drawable from the resources:

  1. for (int i=0; i<jsonArray.length(); ++i) {
  2. JSONObject itemObj = jsonArray.getJSONObject(i);
  3. String name = itemObj.getString("name");
  4. String desc = itemObj.getString("desc");
  5. String imageName = itemObj.getString("image");
  6. // Get drawable by image name
  7. Drawable drawable = getResources().getDrawable(getResources()
  8. .getIdentifier(imageName, "drawable", getPackageName()));
  9. Recipe recipe = new Recipe(name, desc, drawable);
  10. recipes.add(recipe);
  11. }
英文:

Modify image name in the JSON like
{"id":"1", "name":"image1", "desc":"Image Description", "image":"imageNameInTheDrawable"}
Get the drawable from the resources:

  1. for (int i=0; i&lt;jsonArray.length(); ++i) {
  2. JSONObject itemObj = jsonArray.getJSONObject(i);
  3. String name = itemObj.getString(&quot;name&quot;);
  4. String desc = itemObj.getString(&quot;desc&quot;);
  5. String imageName = itemObj.getString(&quot;image&quot;);
  6. // Get drawable by image name
  7. Drawable drawable = getResources().getDrawable(getResources()
  8. .getIdentifier(imageName, &quot;drawable&quot;, getPackageName()));
  9. Recipe recipe = new Recipe(name, desc,drawable);
  10. recipes.add(recipe);
  11. }

huangapple
  • 本文由 发表于 2020年4月8日 05:16:03
  • 转载请务必保留本文链接:https://java.coder-hub.com/61089538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定