3

I have many text files and want to put them in a for loop.

I get an Extra that have Resource name from last activity (s) and have an array that have Resource name of my text files in raw Resource is from {d0,d1,d2,d3, ...,d79} and I want to check name(s) and array name and put the find name to resource! I have error on (res=R.raw.(d[i])) my code :

    int res = 0;
    for (int i = 0; i <=79; i = i + 1) {
        if (s.equals(d[i])){
            res=R.raw.(d[i])
        }
    } 
    inputstream = getResources().openRawResource(res);
A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
hkh114
  • 162
  • 1
  • 3
  • 15
  • 2
    check Matt's answer http://stackoverflow.com/questions/3221603/android-retrieving-all-drawable-resources-from-resources-object – MKJParekh Sep 18 '12 at 07:59

2 Answers2

3

You can use getIdentifier (String name, String defType, String defPackage) for fetching resource id dynamically,

ArrayList<Integer> id = new ArrayList<Integer>();
for (int i = 0; i <= 79; i++) {
  id.add(getResources().getIdentifier("d"+i, "raw", getPackageName()));
}

Now you will have all the resources id's inside id ArrayList

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • i put this , but i get force close ??? int id[] = new int[79]; for (int i = 0; i <= 79; i++) { id[i] = getResources().getIdentifier("d"+i, "raw", getPackageName()); if (s.equals(d[i])) {res=id[i];z=d[i];} } – hkh114 Sep 18 '12 at 10:28
0

I have 6 variations of resources in one place I need to read.

    try
     {
        for (int i = 0; i < 6; i++ )
         {
              String fname = "p" + i;
              int id = context.getResources().getIdentifier(fname, "drawable", "com.example.yourproject");
              if (id == 0)
              {
                  Log.e(TAG, "Lookup id for resource '"+fname+"' failed");
                  // graceful error handling code here
              }
             scoresBm[i] =  (Bitmap) BitmapFactory.decodeResource(context.getResources(), id);
         }
    }
    catch(Exception E)
    {
    }
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300