9

I'm trying to fill a Spinner with the values of some Strings of string.xml. I'm trying to do like that:

   final List<String> list = new ArrayList<String>();
   for (int i = 0; i < 86; i++) {
      String resource = "R.string." + ntpServers[i][0];
      list.add(getResources().getString(resource));
   }

I have strings like the next ones.

<string name="AT">Austria</string>
<string name="BY">Belarus</string>
<string name="BE">Belgium</string>

And from a Geocoder I get a country code like AT, BY, etc. Which are stored in the ntpServers array. I want to fill the spinner with the name saved in the correspondent string, but I cannot do getResources().getString(resource) because resource is a String not an int.

How can I solve this?

Thanks for your advices.

P.D.:

I get the next sequence:

For Example:

geocoder.getCountry() = ES --> ntpServers[36][0] = ES --> R.string.ES = """Spain""" 

Which is the string that I want in the spinner.

____________________________

This is what I'm trying to do now. I'm using getIdentifier.

final List<String> list = new ArrayList<String>();
for (int i = 0; i < 86; i++) {
   String resource = "R.string." + ntpServers[i][0];
   Log.i("RESOURCE", resource);
   Log.i("getResource", "" + getResources().getIdentifier(resource, "string", getPackageName()));
                list.add(getResources().getString(getResources().getIdentifier(resource, "string", getPackageName())));
}

And this is the log cat

09-18 19:32:43.815: I/RESOURCE(31268): R.string.Global
09-18 19:32:43.815: I/getResource(31268): 0
09-18 19:32:43.815: W/ResourceType(31268): No package identifier when getting value for resource number 0x00000000
09-18 19:32:43.815: D/AndroidRuntime(31268): Shutting down VM
09-18 19:32:43.815: W/dalvikvm(31268): threadid=1: thread exiting with uncaught exception (group=0x40fa92a0)
09-18 19:32:43.815: E/AndroidRuntime(31268): FATAL EXCEPTION: main
09-18 19:32:43.815: E/AndroidRuntime(31268): android.content.res.Resources$NotFoundException: String resource ID #0x0
09-18 19:32:43.815: E/AndroidRuntime(31268):    at android.content.res.Resources.getText(Resources.java)
09-18 19:32:43.815: E/AndroidRuntime(31268):    at android.content.res.Resources.getString(Resources.java)
09-18 19:32:43.815: E/AndroidRuntime(31268):    at victor.martin.gplace.MainActivity$3.run(MainActivity.java:790)
09-18 19:32:43.815: E/AndroidRuntime(31268):    at android.os.Handler.handleCallback(Handler.java)
09-18 19:32:43.815: E/AndroidRuntime(31268):    at android.os.Handler.dispatchMessage(Handler.java)
09-18 19:32:43.815: E/AndroidRuntime(31268):    at android.os.Looper.loop(Looper.java)
09-18 19:32:43.815: E/AndroidRuntime(31268):    at android.app.ActivityThread.main(ActivityThread.java)
09-18 19:32:43.815: E/AndroidRuntime(31268):    at java.lang.reflect.Method.invokeNative(Native Method)
09-18 19:32:43.815: E/AndroidRuntime(31268):    at java.lang.reflect.Method.invoke(Method.java)
09-18 19:32:43.815: E/AndroidRuntime(31268):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
09-18 19:32:43.815: E/AndroidRuntime(31268):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
09-18 19:32:43.815: E/AndroidRuntime(31268):    at dalvik.system.NativeStart.main(Native Method)

And this is the R.java:

public static final class string {
        public static final int AE=0x7f080049;
        public static final int AO=0x7f08005b;
        ...
        public static final int Global=0x7f080008;

Let's see if you can guide me more.

Eternal Thanks ^^

Víctor Martín
  • 3,352
  • 7
  • 48
  • 94
  • Take a look at String Array http://developer.android.com/guide/topics/resources/string-resource.html#StringArray in the documentation, that looks more like what you want – Eluvatar Sep 18 '13 at 17:08

5 Answers5

9

Use string-array like

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="item">
        <item>AT</item>
        <item>BY</item>
        <item>BE</item>
    </string-array>
</resources>


Resources res = getResources();
String[] array = res.getStringArray(R.array.item);

convert Array to List

List list = Arrays.asList(array);

/* According to your requirement */ In order to solve your issue use HashMap which keep data in the form of key value pair.

public HashMap<String, String> getCountryCode() {
        HashMap<String, String> countryCode = new HashMap<String, String>();
        countryCode.put("IN", "INDIA");
        countryCode.put("ES", "Spain");
        countryCode.put("USA", "United States of America");

        return countryCode;
    }

String countryName= getCountryCode().get(geocoder.getCountry());

Hope this will solve your problem.

Amit Gupta
  • 8,914
  • 1
  • 25
  • 33
9

As @Henry advised me, I have used getIdentifier with the next code:

list.add(getResources().getString(getResources().getIdentifier(ntpServers[i][0], "string", getPackageName())));

The problem was that the resource name that we need to find didn't needed the prefix "R.string", only the name.

Thanks to all.

Víctor Martín
  • 3,352
  • 7
  • 48
  • 94
1

You can also use the Resources.getIdentifier method (see here) which converts a resource name to the int resource identifier. However, this is slower than using an array.

Henry
  • 42,982
  • 7
  • 68
  • 84
  • I added some info when I have try to use getIdentifier – Víctor Martín Sep 18 '13 at 17:39
  • 1
    @Victor_J_Martin the resource name should not have the "R.string" prefix included. In the example just use "Global". – Henry Sep 18 '13 at 17:54
  • Thanks @Henry it was failing for the prefix "R.string" ;) Thank you so much. This is the correct form: list.add(getResources().getString(getResources().getIdentifier(ntpServers[i][0], "string", getPackageName()))); – Víctor Martín Sep 18 '13 at 18:09
  • I have retaken this project to update the version. But formerly, I used Eclipse IDE, and now I use Android Studio. Now the list.add(...) is marked as error by Android Studio saying me that "Expected resource of type string". I don't know what is the problem. If you can help me, I would be grateful. – Víctor Martín Apr 18 '14 at 18:53
  • 1
    Make it a new question, more detail is needed to identify the problem. – Henry Apr 19 '14 at 05:10
  • Thank, at this moment, It compiles and runs, although the error is there. If later makes me more problems, I will post it. Thanks. – Víctor Martín Apr 19 '14 at 10:07
0

Try this

array in strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="items">
        <item>Austria</item>
        <item>Belarus</item>
        <item>Belgium</item>
    </string-array>
</resources>

To populate spinner

Spinner sp = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(ActivityName.this, R.array.item, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

This is another similar solution but it use the Collections helper class:

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="countries">
        <item>Austria</item>
        <item>Belarus</item>
        <item>Belgium</item>
    </string-array>
</resources>

-

    String[] countryNames = getResources().getStringArray(R.array.countries);
    List<String> countryList = new ArrayList<String>();

    Collections.addAll(countryList, countryNames); 
Diego Palomar
  • 6,958
  • 2
  • 31
  • 42