0

I had a problem where I needed to map some keys received from a website to image id's in my android app.

The solution I initally came up with was to use switch statements like so:

switch (weather_type) {
    case "cloudy":
        return R.drawable.cloudy;
    case "rainy":
        return R.drawable.rainy;

    // etc
}

This is good and works great, but I was thinking if there was a way to store this information in XML and have it available at runtime. I was thinking something like a map in XML that lets me map the names to the actual resource, but since I'm still learning android, I'm not sure exactly how to go about doing this.

My plan then involved creating a string-array in xml to retrieve the values.

<string-array name="icon_map">
    <item>cloudy:@mipmap/cloudy</item>
    <item>rainy:@mipmap/rain</item>
</string-array>

In Java:

String []array = getResources().getStringArray(R.array.icon_map);
Log.i(TAG, array[0]);

Results in

1798-1798/com.practice.smac89.webstuff I/MainActivity cloudy:@mipmap/cloudy

When I was typing this in Android Studio, it was showing the actual images when I hover my mouse over the resources. So I was hoping that when I get this in the activity class, it would have been converted to the actual Id, but it wasn't.

TL;DR

Is there a way to store a mapping in xml where the value is a resource id and get the actual integer id in java? I was hoping for

1798-1798/com.practice.smac89.webstuff I/MainActivity cloudy:1231243

1
  • 1
    I think your original idea sounds like the best option, create a HashMap<String, Integer>, and have a method that populates it once when your app starts up. Commented May 23, 2015 at 0:45

2 Answers 2

1
TypedArray ar = context.getResources().obtainTypedArray(R.array.my_array);
int len = ar.length();
int[] resIds = new int[len];
for (int i = 0; i < len; i++)
    resIds[i] = ar.getResourceId(i, 0);
ar.recycle();
// Do stuff with resolved reference array, resIds[]...
for (int i = 0; i < len; i++)
    Log.v (TAG, "Res Id " + i + " is " + Integer.toHexString(resIds[i]));
Sign up to request clarification or add additional context in comments.

Comments

0

Yes, I think you can get what you want by using string array and integer array at the same time, like below, in XML you define two arrays.

<string-array name="name_array">
    <item>cloudy</item>
    <item>rainy</item>
</string-array>

<array name="icon_array">
    <item>@mipmap/cloudy</item>
    <item>@mipmap/rain</item>
</array>

In Java, you can do

String [] nameArray = getResources().getStringArray(R.array.name_array);

int[] iconArray = getResources().getIntArray(R.array.icon_array);

for(int i=0; i<nameArray.length; i++){

      if(nameArray[i].equals(weather_type)){

           return iconArray[i])
      }    
}

Here is my sample of string array and drawable array.

<string-array name="settings_titles">

<item>@string/TITLE_SERVICES</item>

<item>@string/TITLE_USERACCOUNTS</item>

<item>@string/TITLE_ABOUT</item>

</string-array>


<array name="settings_icons">

<item>@drawable/ic_android_services</item>

<item>@drawable/ic_android_users</item>

<item>@drawable/ic_android_about</item>

</array>

and you also can use TypedArray to access the resource. like

final TypedArray mTitles = getResources().obtainTypedArray(titlesId);

1 Comment

my bad, the iconArray is not a String Array, should be a IntArray.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.