2

im trying to resolve the following problem : i named my string resource like that in my xml string file :

 <string name="array_nature">Nature</string>
<string name="array_architecture">Architecture</string>
<string name="array_arts">Art/Literature</string>
<string name="array_cinema">Cinema/TV/Radio</string>
<string name="array_geography">Geographie</string>
<string name="array_leisure">Loisirs</string>
<string name="array_music">Musique</string>
<string name="array_people">People</string>
<string name="array_sciences">Sciences</string>
<string name="array_technology">Technologie</string>


<string-array name="array_category">
    <item>@string/array_nature</item>
    <item>@string/array_architecture</item>
    <item>@string/array_arts</item>
    <item>@string/array_cinema</item>
    <item>@string/array_geography</item>
    <item>@string/array_leisure</item>
    <item>@string/array_music</item>
    <item>@string/array_people</item>
    <item>@string/array_sciences</item>
    <item>@string/array_technology</item>
</string-array>

i also have string-arrays corresponding to those two strings :

<string-array name="array_nature">
        <item>Species</item>
        <item>Plant</item>
        <item>Animal</item>
        <item>CelestialBody</item>
        <item>Asteroid</item>
        <item>Galaxy</item>
        <item>Planet</item>
        <item>Satellite</item>
        <item>Star</item>
    </string-array>

<string-array name="array_architecture">
        <item>AmusementParkAttraction</item>
        <item>Building</item>
        <item>MilitaryStructure</item>
        <item>Infrastructure</item>
        <item>Airport</item>
        <item>RouteOfTransportation</item>
        <item>Bridge</item>
        <item>HistoricPlace</item>
        <item>Monument</item>
        <item>Park</item>
        <item>Zoo</item>
    </string-array>

as you can see, the attribute name of my string (name = "array_nature") is exactly the same as the string-array name.

My goal is to avoid an annoying if, else if, else if ... code in my activity... So is that possible to get Something like that in my java code?

int myStringArray = R.id.+getResourceEntryName(R.string.array_nature);

and i stuck in this method

public void getchoicearray() {

        Spinner sp = ((Spinner)findViewById(R.id.spinner_category));
        int id = getResources().getIdentifier(getResources().getResourceEntryName(sp.getSelectedItem()./*how to get the entry
        name of selected spinner item*/), "array", getPackageName());

        choiceArray = id;

ArrayAdapter a = ArrayAdapter.createFromResource(this, choiceArray, android.R.layout.simple_spinner_item);
        ((Spinner) findViewById(R.id.spinner_choice)).setAdapter(a);

}

or any other value corresponding to a spinner choice...

EDIT : added xml spinner content

Noj
  • 164
  • 1
  • 15
  • 2
    `int myStringArray = getResources().getIdentifier("array_nature", "array", getPackageName())` ? – Blackbelt Oct 16 '15 at 10:02
  • Use a `Switch` statement instead . – vguzzi Oct 16 '15 at 10:07
  • i cant if i switch it need to be a constant... and also i try to make it in one line, imagine if your choices are over 100? or even 1000, so you have to make a switch or if else state of 1000 line, boring... @Blackbelt what is getpackagename (third argument) ? – Noj Oct 16 '15 at 10:32
  • returns the package name of your app. Is a method of Context – Blackbelt Oct 16 '15 at 10:39
  • ok @Blackbelt thanks, but i cant get cleary what i need to write instead of "array" and arraynature? – Noj Oct 16 '15 at 10:49
  • do you want to retrieve the id of the string-array having its name as variable, did I understand correctly ? – Blackbelt Oct 16 '15 at 10:51
  • yes i want too change my second spinner entries at runtime, when user choose a category from 1st spinner, the other spinner needs to have corresponding entries (stored in string-array) (e.g -> user choose nature in 1st spinner, 2nd spinner entries changes to array_nature entries) – Noj Oct 16 '15 at 10:54

2 Answers2

2

you can use public int getIdentifier (String name, String defType, String defPackage) to retrive the id at runtime.

  1. name is the name of the resource. The one you specified in the the name attribute in the xml
  2. defType is the type of the resource. E.g. drawable. In your case you have to use "array", because the resource you want is an array.
  3. defPackage, is the package name of your app. You can retrieve it using getPackageName().

If you want to retrieve the id of array_nature, you will have:

int myStringArray = getResources().getIdentifier("array_nature", "array", getPackageName())

getIdentifier returns 0 it the resource can't be found. So you should always check if the return value is grater than 0

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • oh ok, i got it thanks a lot... but how to get array_nature from my first spinner? my array_nature is referenced into thi 1st spinner (here i want to have -> myspinner.getselecteditem().getresourcentryname() ) ^^ – Noj Oct 16 '15 at 11:45
  • 1
    could you explain a little bit more about the structure. Maybe posting some code ? – Blackbelt Oct 16 '15 at 11:47
  • is getSelectedItem returning Nature or Architecture ? – Blackbelt Oct 16 '15 at 11:52
  • i edited my question with more xml and the method i want to make according to my initial question – Noj Oct 16 '15 at 11:57
  • it would much easier to use another approach for your use case. Would you like me to explain it ? – Blackbelt Oct 16 '15 at 12:42
0
Resources res = getResources();
String[] planets = res.getStringArray(R.array.array_nature);

Do the above one in strings.xml.

Shivani Gupta
  • 271
  • 3
  • 12