英文:
Accessing drawable file from String Arraylist in android
问题
我对Android开发还不太熟悉,但我正在尝试解析一个包含如下JSON对象的本地JSON文件:
"pigs": [
{
"name": "Duroc",
"list_image": "duroc_list",
"desc_image": "duroc_desc",
"description": "Duroc猪是一种较早的家养猪品种。该品种在美国得到发展,并成为许多杂交商业猪的基础。Duroc猪呈红褐色,体型大,中等长度,肌肉发达,耳朵部分下垂。它们往往是为肉食而饲养的所有猪品种中最不具攻击性的品种之一。"
},
我需要从“list_image”中将图片存储到RecyclerView中,当点击图片时,其余信息存储在单独的活动中。
当图片存储在本地drawable文件夹中时,我应该如何使图片显示在RecyclerView中呢?
非常感谢您的帮助!谢谢!!!
编辑:
在将每个图像文件的名称存储在一个数组中后,例如:“duroc_list”:
ArrayList<String> list_image = new ArrayList<>();
ArrayList<String> name = new ArrayList<>();
ArrayList<String> desc_image = new ArrayList<>();
ArrayList<String> description = new ArrayList<>();
try {
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONArray pigArray = obj.getJSONArray("pigs");
for (int i = 0; i < pigArray.length(); i++) {
JSONObject pigDetail = pigArray.getJSONObject(i);
name.add(pigDetail.getString("name"));
list_image.add(pigDetail.getString("list_image"));
desc_image.add(pigDetail.getString("desc_image"));
description.add(pigDetail.getString("description"));
}
} catch (JSONException e) {
e.printStackTrace();
}
如何选择数组中的每个项目并在适配器中使用,以在RecyclerView中查看它们呢?
适配器:
List<Integer> images;
LayoutInflater inflater;
public Adapter(Context context, List<Integer> images) {
this.images = images;
this.inflater = LayoutInflater.from(context);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.grid_layout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.listPigs.setImageResource(images.get(position));
}
@Override
public int getItemCount() {
return images.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView listPigs;
public ViewHolder(@NonNull View itemView) {
super(itemView);
listPigs = itemView.findViewById(R.id.image_list);
}
}
英文:
I'm pretty new to Android development but I am trying to parse a local JSON file with JSON objects like this:
"pigs": [
{
"name": "Duroc",
"list_image": "duroc_list",
"desc_image": "duroc_desc",
"description": "The Duroc pig is an older breed of domestic pig. The breed was developed in the United States and formed the basis for many mixed-breed commercial hogs. Duroc pigs are reddish-brown, large-framed, medium length, and muscular, with partially-drooping ears. They tend to be one of the least aggressive of all swine breeds raised for meat."
},
and I need to store the images into a RecyclerView specifically from "list_image" and the rest of the information is stored on separate activities when the image is clicked.
How would I go about getting the image to go into the RecyclerView when the image is stored in a local drawable folder?
Any help would be greatly appreciated! Thanks!!!
EDIT:
After having the names of each image file stored in an array Ie: "duroc_list"
ArrayList<String> list_image = new ArrayList<>();
ArrayList<String> name = new ArrayList<>();
ArrayList<String> desc_image = new ArrayList<>();
ArrayList<String> description = new ArrayList<>();
try {
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONArray pigArray = obj.getJSONArray("pigs");
for (int i = 0; i < pigArray.length(); i++) {
JSONObject pigDetail = pigArray.getJSONObject(i);
name.add(pigDetail.getString("name"));
list_image.add(pigDetail.getString("list_image"));
desc_image.add(pigDetail.getString("desc_image"));
description.add(pigDetail.getString("description"));
}
} catch (JSONException e) {
e.printStackTrace();
}
How would each item in the array be selected and used in an adapter to be viewed in a RecyclerView?
Adapter:
List<Integer> images;
LayoutInflater inflater;
public Adapter(Context context, List<Integer> images) {
this.images = images;
this.inflater = LayoutInflater.from(context);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.grid_layout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.listPigs.setImageResource(images.get(position));
}
@Override
public int getItemCount() {
return images.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView listPigs;
public ViewHolder(@NonNull View itemView) {
super(itemView);
listPigs = itemView.findViewById(R.id.image_list);
}
}
答案1
得分: 1
你可以通过保存的名称从drawable资源文件夹中获取drawable。例如,如果在你的res/drawable
文件夹中有ic_home.png
,你可以将"ic_home"
作为该方法的第一个参数传递来获取它。
public static Drawable getDrawable(String name, Context context) {
int resourceId = context.getResources().getIdentifier(name, "drawable", context.getPackageName());
return context.getResources().getDrawable(resourceId);
}
英文:
You can get drawable from your drawable res folder by the saved name. For example if you have ic_home.png
in your res/drawable
folder you can get that passing "ic_home"
as the first parameter of this method.
public static Drawable getDrawable(String name, Context context) {
int resourceId = context.getResources().getIdentifier(name, "drawable", context.getPackageName());
return context.getResources().getDrawable(resourceId);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论