Can't I do getResources.getString dynamically?

I am trying to populate Spinner values โ€‹โ€‹of some Strings string.xml . I am trying to do this:

  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 lines similar to the following.

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

And from the geocoder I get a country code, such as AT, BY, etc., which are stored in the ntpServers array. I want to fill the counter with the name stored in the corresponding line, but I cannot do getResources().getString(resource) , because resource is String not a int .

How can i solve this?

Thanks for your advice.

PD :.

I get the following sequence:

Example:

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

What is the line I want in spinner.

____________________________

This is what I'm trying to do now. I am 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 a cat log

 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 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 find out more.

Thank you forever ^^

+6
source share
6 answers

As @Henry told me, I used getIdentifier with the following code:

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

The problem was that the name of the resource we need to find did not require the prefix "R.string", only the name.

Thanks to everyone.

+4
source

Use a 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 * / To solve your problem, use HashMap, which store data in the form of a pair of key values.

 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 solves your problem.

+9
source

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

+1
source

try it

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> 

Spinner filling

 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); 
0
source

This is another similar solution, but it uses 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); 
0
source

An easy way to get a resource identifier from a string. Here, resourceName is the name of the ImageView resource in the transferable folder, which is also included in the XML file.

 int resID = getResources().getIdentifier(resourceName, "id", getPackageName()); ImageView im = (ImageView) findViewById(resID); Context context = im.getContext(); int id = context.getResources().getIdentifier(resourceName, "drawable", context.getPackageName()); im.setImageResource(id); 
0
source

Source: https://habr.com/ru/post/954089/


All Articles