I had this, which was working fine:
public static Integer[] photos = new Integer[]
{R.drawable.photo1,R.drawable.photo2,R.drawable.photo3};
this.setImageResource(photos[mCellNumber]);
But I decided I wanted to put the filenames in an XML file instead, which I did, like this:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<array name="Red">
<item>R.drawable.photo1</item>
<item>R.drawable.photo2</item>
<item>R.drawable.photo3</item>
</array>
</resources>
And tried stuff like this:
String[] month = getResources().getStringArray(R.array.Red);
this.setImageResource(month[mCellNumber]);
..and..
String[] month = getResources().getStringArray(R.array.Red);
int bla = Integer.parseInt(month[mCellNumber]);
this.setImageResource(bla);
I understand why it’s not working (strings/ints), but I haven’t found any simple way of handling the string to integer conversion part or alternatively, using setImageResource with a string as parameter. Any suggestions?
looks it will throw exception R.drawable.photo1 as a string is not a valid number.
You an try with only saving name like photo1,photo2,photo3 and then do as below:
…
Android – Store drawable ids in a custom XML file
you can try with only saving the name like “joe_pic” and can get this way
..
but if you have already added “R.drawable.joe_pic” complete can use String.split to get 3 rd string part