访问 Android 中 String ArrayList 中的可绘制文件

huangapple go评论83阅读模式
英文:

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:

     &quot;pigs&quot;: [
{
  &quot;name&quot;: &quot;Duroc&quot;,
  &quot;list_image&quot;: &quot;duroc_list&quot;,
  &quot;desc_image&quot;: &quot;duroc_desc&quot;,
  &quot;description&quot;: &quot;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.&quot;
},

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&lt;String&gt; list_image = new ArrayList&lt;&gt;();
    ArrayList&lt;String&gt; name = new ArrayList&lt;&gt;();
    ArrayList&lt;String&gt; desc_image = new ArrayList&lt;&gt;();
    ArrayList&lt;String&gt; description = new ArrayList&lt;&gt;();

    try {
        JSONObject obj = new JSONObject(loadJSONFromAsset());
        JSONArray pigArray = obj.getJSONArray(&quot;pigs&quot;);

        for (int i = 0; i &lt; pigArray.length(); i++) {

            JSONObject pigDetail = pigArray.getJSONObject(i);

            name.add(pigDetail.getString(&quot;name&quot;));
            list_image.add(pigDetail.getString(&quot;list_image&quot;));
            desc_image.add(pigDetail.getString(&quot;desc_image&quot;));
            description.add(pigDetail.getString(&quot;description&quot;));

        }
    } 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&lt;Integer&gt; images;
LayoutInflater inflater;

public Adapter(Context context, List&lt;Integer&gt; 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,你可以将&quot;ic_home&quot;作为该方法的第一个参数传递来获取它。

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 &quot;ic_home&quot; as the first parameter of this method.

public static Drawable getDrawable(String name, Context context) {
    int resourceId = context.getResources().getIdentifier(name, &quot;drawable&quot;, context.getPackageName());
    return context.getResources().getDrawable(resourceId);
}

huangapple
  • 本文由 发表于 2020年4月8日 10:36:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/61092479.html
匿名

发表评论

匿名网友

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

确定