1

I have in my database a column that stores the filename of the images I'm using in the android application. In most of my activities I need to use a lot of these images.

So I store these filenames in an ArrayList<String>and I want for my output to be an ArrayList<Integer> of Resources IDs of these images.

Based on several questions here on stackoverflow, I used the following technique (got it from this question) :

//  Where tileImageFilenames is the ArrayList<String>

    for(int i=0;i<tileImagesFilename.size();i++)
    {

        tileImagesIDS.add(getResources().getIdentifier(tileImagesFilename.get(i) , "drawable", getApplication().getPackageName()));
    }

This returns me an ArrayList<Integer> full of 0s.

Community
  • 1
  • 1
tvieira
  • 1,865
  • 3
  • 28
  • 45

2 Answers2

2

you must create new object and then add it to your ArrayList<Integer>

for(int i=0;i<tileImagesFilename.size();i++)
    {
        String name =  tileImagesFilename.get(i).replace(".jpg","");
        tileImagesIDS.add(new Integer(getResources().getIdentifier(name , "drawable", getApplication().getPackageName())));
    }
tvieira
  • 1,865
  • 3
  • 28
  • 45
mmlooloo
  • 18,937
  • 5
  • 45
  • 64
  • 1
    from what I understand generics in java is not applicable to permitives, so ArrayList is definite fail. – Raf Sep 01 '14 at 14:58
  • do you add `new Integer` ? – mmlooloo Sep 01 '14 at 15:12
  • Thanks this worked for me. @mmlooloo please do you think you could give me your opinion on this question http://stackoverflow.com/questions/25598696/recommended-way-order-to-read-data-from-a-webservice-parse-that-data-and-inse – Axel Sep 02 '14 at 03:31
0

Try using the following code, if it worked for you then you can put it in your for loop.

String mDrawableName = "myappicon";
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
Raf
  • 7,505
  • 1
  • 42
  • 59
  • Thank for the down vote, if you would be kind enough to explain that would help me understand where I went wrong. – Raf Sep 01 '14 at 14:51
  • That's pretty much what I'm using right now – tvieira Sep 01 '14 at 14:52
  • Well, you have not clearly mentioned in your question what is the content of your ArrayList that hold your filenames. Maybe it has files with extension. It has to be just the filename, with no extension. I wanted to say that your question is not complete and that you need to provide more information but, you seem to be very quick with pressing the down vote. – Raf Sep 01 '14 at 14:53
  • wasn't me who downvoted ;) (I don't have rep points enough to do it yet) – tvieira Sep 01 '14 at 14:59
  • 1
    Never mind, I can afford a few down votes. Have a look to these links http://stackoverflow.com/questions/3476430/how-to-get-a-resource-id-with-a-known-resource-name http://stackoverflow.com/questions/3042961/how-can-i-get-the-resource-id-of-an-image-if-i-know-its-name – Raf Sep 01 '14 at 15:03
  • +1 because you helped me !!1 – mmlooloo Sep 01 '14 at 15:27
  • @mmlooloo How did I help you? the generic to primitives? – Raf Sep 01 '14 at 16:14