英文:
i cant show image from drawable in my app using JSON
问题
我已经正确地从资源文件(recipe.json)中读取了我的 JSON 数据,该数据包含:
{
"id":"1",
"name":"الوصفة 1",
"desc":"الوصفة 1",
"image":"R.drawable.image"
}
我在 RecyclerView 中正确地加载了名称和描述,但是使用 getIdentifier()
方法无法显示图像...这是我的代码:
private void addItemsFromJSON() {
try {
String jsonDataString = readJSONDataFromFile();
JSONObject jsonObject = new JSONObject(jsonDataString);
JSONArray jsonArray = jsonObject.getJSONArray("recipes");
for (int i = 0; i < jsonArray.length(); ++i) {
JSONObject itemObj = jsonArray.getJSONObject(i);
String name = itemObj.getString("name");
String desc = itemObj.getString("desc");
String image = itemObj.getString("image");
// 通过图像名称获取资源ID
int resourceId = getResources().getIdentifier(image, "drawable", getPackageName());
Recipe recipe = new Recipe(name, desc, resourceId);
recipes.add(recipe);
}
} catch (JSONException | IOException e) {
Log.d(TAG, "addItemsFromJSON: ", e);
}
}
它会显示标题和描述,但图像为空。
英文:
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
private void addItemsFromJSON() {
try {
String jsonDataString = readJSONDataFromFile();
JSONObject jsonObject = new JSONObject(jsonDataString);
JSONArray jsonArray=jsonObject.getJSONArray("recipes");
for (int i=0; i<jsonArray.length(); ++i) {
JSONObject itemObj = jsonArray.getJSONObject(i);
String name = itemObj.getString("name");
String desc = itemObj.getString("desc");
String image = itemObj.getString("image");
// get resource id by image name
int resourceId =getResources().getIdentifier(image, "drawable",getPackageName());
Recipe recipe = new Recipe(name, desc,resourceId);
recipes.add(recipe);
}
} catch (JSONException | IOException e) {
Log.d(TAG, "addItemsFromJSON: ", e);
}
}
it shows the title and describtion but image is empty
答案1
得分: 0
Modify image name in the JSON like
{"id":"1", "name":"image1", "desc":"Image Description", "image":"imageNameInTheDrawable"}
Get the drawable from the resources:
for (int i=0; i<jsonArray.length(); ++i) {
JSONObject itemObj = jsonArray.getJSONObject(i);
String name = itemObj.getString("name");
String desc = itemObj.getString("desc");
String imageName = itemObj.getString("image");
// Get drawable by image name
Drawable drawable = getResources().getDrawable(getResources()
.getIdentifier(imageName, "drawable", getPackageName()));
Recipe recipe = new Recipe(name, desc, drawable);
recipes.add(recipe);
}
英文:
Modify image name in the JSON like
{"id":"1", "name":"image1", "desc":"Image Description", "image":"imageNameInTheDrawable"}
Get the drawable from the resources:
for (int i=0; i<jsonArray.length(); ++i) {
JSONObject itemObj = jsonArray.getJSONObject(i);
String name = itemObj.getString("name");
String desc = itemObj.getString("desc");
String imageName = itemObj.getString("image");
// Get drawable by image name
Drawable drawable = getResources().getDrawable(getResources()
.getIdentifier(imageName, "drawable", getPackageName()));
Recipe recipe = new Recipe(name, desc,drawable);
recipes.add(recipe);
}
专注分享java语言的经验与见解,让所有开发者获益!
评论