Q:

How to get the ID of an image from a string array

I created a custom adapter for my GridView that will display an image of a book (View image) and its name (View text);
, I declared the list of names of the books in the string file:
   [strings.xml]
<string-array name="bookLabels">

    <item>android security</item>
    <item>penetration_testing</item>
    <item>red_team</item>
    <item>Linux Firewalls</item>
    <item>the_art_of_exploitation</item>
    <item>web_application_hacker</item>
    <item>Linux Basicsfor Hackers</item>
    <item>Black Hat Python</item>

</string-array>

So I want to do the same thing with the images,instead of declaring them in the code like that :
 private Integer[] bookImages =
        {
                R.drawable.android_security,
                R.drawable.penetration_testing,
                R.drawable.red_team,
                R.drawable.lf,
                R.drawable.the_art_of_exploitation,
                R.drawable.web_application_hacker,
                R.drawable.lbh,
                R.drawable.bhp

        };

I declared the names of the images in a string array named "bookImages", 
  <string-array name="bookImages">
    <item>@drawable/android_security</item>
    <item>@drawable/penetration_testing</item>
    <item>@drawable/red_team</item>
    <item>@drawable/lf</item>
    <item>@drawable/the_art_of_exploitation</item>
    <item>@drawable/web_application_hacker </item>
    <item>@drawable/lbh </item>
    <item>@drawable/bhp </item>
 </string-array>

and then I recovered the location of all the images declared in  to "bookimagesfromstring"array :
Resources resi = context.getResources();
bookImagesFromString = resi.getStringArray(R.array.bookImages);
System.out.println("bookImages Images test 1: " + 
Arrays.toString(bookImagesFromString))

this is the source code of [main activity]
public class main extends AppCompatActivity {
String[] labels;
String[ ] pages;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    Resources res = getResources();
    labels = res.getStringArray(R.array.bookLabels);
    pages = res.getStringArray(R.array.web_pages);

    GridView gridView = findViewById(R.id.gridView1);
    BookGrid myAdapter = new BookGrid(getApplicationContext(), labels);
    gridView.setAdapter(myAdapter);

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {

            Toast.makeText(
                    getApplicationContext(),
                    ((TextView) v.findViewById(R.id.gridtextView))
                            .getText(), Toast.LENGTH_SHORT).show();

           // Intent webPageIntent = new Intent(getApplicationContext(), 
          DisplayWebPage.class);
          //  webPageIntent.putExtra("WEB_PAGE", pages[ position ] );

           // startActivity( webPageIntent );

        }
    });
}

}
And the [ BookGrid Class ]
public class BookGrid extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private String[] imageName;
private String[] bookImagesFromString;

private Integer[] bookImages =
        {
                R.drawable.android_security,
                R.drawable.penetration_testing,
                R.drawable.red_team,
                R.drawable.lf,
                R.drawable.the_art_of_exploitation,
                R.drawable.web_application_hacker,
                R.drawable.lbh,
                R.drawable.bhp

        };

public BookGrid(Context context, String[] imageName) {
    this.context = context;
    this.imageName = imageName;
    this.inflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    return bookImages.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    /*
    Resources rest = context.getResources();
    String[] labels = rest.getStringArray(R.array.bookLabels);
    System.out.println("bookImagess Tex: " + Arrays.toString(labels));

    */
    Resources resi = context.getResources();
    bookImagesFromString = resi.getStringArray(R.array.bookImages);
    System.out.println("bookImages Images test 1: " + Arrays.toString(bookImagesFromString));

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.layout_grid_image, parent, false);
        TextView txtv = convertView.findViewById(R.id.gridtextView);
        ImageView imgv = convertView.findViewById(R.id.gridImageView);

        //To Avoid ArrayIndexOutOfBoundsException
        //boolean inBounds = (position >= 0) && (position <= imageName.length);

            txtv.setText(imageName[position]);

      //imgv.setImageResource(bookImagesFromString[position]);
        imgv.setImageResource(bookImages[position]);

    } else {

    }

    return convertView;
}

the result for the "bookImagesFromString" array was as follows :
([res/drawable-v24/android_security.png, res/drawable-v24/penetration_testing.png, res/drawable-v24/red_team.png, res/drawable/lf.jpg, res/drawable-v24/the_art_of_exploitation.png, res/drawable-v24/web_application_hacker.png, res/drawable/lbh.png, res/drawable-v24/bhp.png])
So the question is how to convert the "bookImagesFromString" array to an array that contains the ID of each image ? -And after using it in this code part:
 imgv.setImageResource(bookImagesID[position]); 

Thanks.

A:

Try string array like this:
<string-array name="bookImages">
    <item>android_security</item>
    <item>penetration_testing</item>
    <item>red_team</item>
    <item>lf</item>
    <item>the_art_of_exploitation</item>
    <item>web_application_hacker</item>
    <item>lbh</item>
    <item>bhp</item>
</string-array>

and adapter like this:
public class BookGrid extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private String[] imageName;
private Integer[] bookImages;

public BookGrid(Context context, String[] imageName) {
    this.context = context;
    this.imageName = imageName;
    this.inflater = LayoutInflater.from(context);

    Resources resi = context.getResources();
    String[] bookImagesFromString = resi.getStringArray(R.array.bookImages);
    bookImages = new Integer[bookImagesFromString.length];
    for (int i = 0; i < bookImagesFromString.length; i++) {
        bookImages[i] = (resi.getIdentifier(bookImagesFromString[i], "drawable", context.getPackageName()));
    }
}

@Override
public int getCount() {
    return imageName.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
/*
Resources rest = context.getResources();
String[] labels = rest.getStringArray(R.array.bookLabels);
System.out.println("bookImagess Tex: " + Arrays.toString(labels));
*/
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.layout_grid_image, parent, false);
    }
    TextView txtv = convertView.findViewById(R.id.gridtextView);
    ImageView imgv = convertView.findViewById(R.id.gridImageView);

    txtv.setText(imageName[position]);
    imgv.setImageResource(bookImages[position]);

    return convertView;
}
}

Hope that helps!

