Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android getResources().getStringArray() variables

Tags:

I have an xml file, where there are strings and string-arrays. I have a class where I click on a listitem, and in the other class I list other items. In the xml there are string which names are the clicked items modified name, and the values are the string-arrays name. I have found a solution how to add a variable to the getResources(9.getStringArray() but it doesn't work. The program starts, but when I click any of the listitem my activity just stops working. My class file:

String artistpicked = extras.getString("artist"); String[] firstW = artistpicked.split(" "); firstW[0] = firstW[0].trim(); String albumSearch = firstW[0] + "_code"; int getRes = getResources().getIdentifier(albumSearch, "string", getPackageName()); String setRes = String.valueOf(getRes); int getRes2 = getResources().getIdentifier(setRes, "array", getPackageName()); String[] albums = getResources().getStringArray(getRes2); setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, albums)); TextView t = (TextView) findViewById(R.id.albumName); t.setText(setRes); 

The TextView is there to check the variables. The albumSearch gives "modifiedartistpicked_code", it's good. The getRes gives the value of from the xml ("something_array"), the setRes gives it's actual id number (which the getStringArray requires). The getRes2 gives the same as getRes it's just there to check that it works fine. When I comment out the next two lines, the String albums[], and setListAdapter then the program works but it doesn't list the items.

The xml file:

<?xml version="1.0" encoding="utf-8"?> <resources>     <string-array name="artists_array">         <item>code1 example</item>         <item>code2 example</item>     </string-array>     <string-array name="code1_array">         <item>item1</item>         <item>item2</item>         <item>item3</item>     </string-array>     <string-array name="code2_array">         <item>item1</item>         <item>item2</item>         <item>item3</item>     </string-array>     <string name="code1_code">code1_array</string>     <string name="code2_code">code2_array</string> </resources> 

I hope I was able to write what I would like to do, and what is the problem :)

Update:

@Marc Bernstein: Because I don't know it's code1_array. First I made many if and when the picked item was x, then I read the R.array.x_array, etc...

But I have got what was the problem, there were capital letters int he name of the strings in xml. Thats why I hate xml, the problem is always there :) This xml was just an example, the original is much greater that's why no one was able to help. Next time I will be much more cautious.

And also I made it now more simplier because you have right it was too complicated.

like image 523
matthew3r Avatar asked Apr 18 '11 15:04

matthew3r


People also ask

How to add special characters in string xml in Android?

How can I write character & in the strings. xml? In android studio, you can simply press Alt+Enter and it will convert for you.

How to get string array from string in Android?

xml you should have key for that String . But in the above you have key for String array so you have to get String array from that you have to get required String. CurrentWebpage = getResources(). getStringArray(R.

What is@ string in Android studio?

A single string that can be referenced from the application or from other resource files (such as an XML layout). Note: A string is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file).


2 Answers

The culprit is most likely

String[] albums = getResources().getStringArray(getRes2); 

Instead of doing

int getRes2 = getResources().getIdentifier(setRes, "array", getPackageName()); String[] albums = getResources().getStringArray(getRes2); 

Why not just use

String[] albums = getResources().getStringArray(R.array.code1_array); 

?

Anyway, from what you posted, I don't think the "array" identifier actually exists within your strings.xml file. I see a code1_array & code2_array.

like image 93
Marc Bernstein Avatar answered Sep 23 '22 11:09

Marc Bernstein


It's because it tries to determine dynamically the array it wants to use. The id from String[] albums = getResources().getStringArray(R.array.code1_array); is given, let say, at user input, some spinner/edittext box. This is instead of using a database, just using some strings from xml file.

like image 21
kredo Avatar answered Sep 23 '22 11:09

kredo