-1

I have multiple string-array in the file string.xml in my Android Project.

<string-array name="SS"> 
  <item>a</item> 
  <item>b</item> 
  <item>c</item>
</string-array> 
<string-array name="SV">
   <item>d</item> 
   <item>e</item> 
   <item>f</item> 
</string-array>

In my activity, I receive a value for string

Bundle extras = getIntent().getExtras();
    if(extras != null){
        mData = (HashMap<String, Object>) extras.get("data");
        this.id = (Integer) this.mData.get("id");
        this.name = (String) this.mData.get("name");

    }

   String[] mValues = getResources().getStringArray("");

"name" can be "SS" or "SV".

How can later find specific string-array item if "name" is "SS"?

NOTE: Currently I use

if(name.equals("SS")){
   String[] mValues = getResources().getStringArray(R.array.SS);
}else if(name.equals("SV")){
   String[] mValues = getResources().getStringArray(R.array.SV);
}

but, for me, is not a good idea and inefficient.

Moises Portillo
  • 828
  • 8
  • 12

2 Answers2

1

Maybe this page can help you Android, getting resource ID from string?

public static int getResId(String variableName, Class<?> c) {

try {
    Field idField = c.getDeclaredField(variableName);
    return idField.getInt(idField);
} catch (Exception e) {
    e.printStackTrace();
    return -1;
} 
Community
  • 1
  • 1
Jérémy
  • 263
  • 1
  • 3
  • 10
0

Create a map similar to

Map<String, Object> map = new HashMap<String, Object>(); // Replace Object with correct type
map.put("SS", R.array.SS);
map.put("SV", R.array.SV);

// ...

String[] mValues = getResources().getStringArray(map.get(name));
Smutje
  • 17,733
  • 4
  • 24
  • 41