I have been unable to find a solution for this.

What I'm trying to do: I have preferences where you can enable/disable what items will show up on the menu. There are 17 items. I made a string array in values/arrays.xml with titles for each of these 17 items.

I have preferences.xml which has the layout for my preferences file, and I would like to reference a single item from the string array to use as the title.

How, if it's possible, can I do this?

In the Android developer reference, I see how I can reference a single string with XML, but now how I can reference a string from an array resource in XML.

Thanks

share|improve this question
feedback

3 Answers

up vote 45 down vote accepted

In short: I don't think you can, but there seems to be a workaround:.

If you take a look into the Android Resource here:

http://developer.android.com/guide/topics/resources/string-resource.html

You see than under the array section (string array, at least), the "RESOURCE REFERENCE" (as you get from an XML) does not specify a way to address the individual items. You can even try in your XML to use "@array/yourarrayhere". I know that in design time you will get the first item. But that is of no practical use if you want to use, let's say... the second, of course.

HOWEVER, there is a trick you can do. See here:

Referencing an XML string in an XML Array (Android)

You can "cheat" (not really) the array definition by addressing independent strings INSIDE the definition of the array. For example, in your strings.xml:

<string name="earth">Earth</string>
<string name="moon">Moon</string>

<string-array name="system">
    <item>@string/earth</item>
    <item>@string/moon</item>
</string-array>

By using this, you can use "@string/earth" and "@string/moon" normally in your "android:text" and "android:title" XML fields, and yet you won't lose the ability to use the array definition for whatever purposes you intended in the first place.

Seems to work here on my Eclipse. Why don't you try and tell us if it works? :-)

share|improve this answer
Thanks, this is what I did ;) I was in the middle of typing my answer when you posted yours. Thanks! – Jorsher Nov 12 '10 at 4:40
Can you please accept the answer by clicking on the green tick? :-P – David Cesarino Nov 12 '10 at 4:43
And by the way, you're welcome. I think, too, that Android should have a way to address an array in XML without resorting to that verbose workaround. Something like "@array/yourarray(1)" But at least it works. – David Cesarino Nov 12 '10 at 4:59
2  
if we could only give name tags to <item>s...*sigh* – Some Noob Student Feb 28 '11 at 7:58
1  
Just tried it! Defined the array in the default strings.xml but didn't in values-fr and the array still updated with the french version. So wrong .. but cool it works! – Blundell Mar 7 at 22:57
show 4 more comments
feedback

Maybe this would help:

String[] some_array = getResources().getStringArray(R.array.your_string_array)

So you get the array-list as a String[] and then choose any i, some_array[i].

share|improve this answer
I understand how to get the array resource. That doesn't allow me to reference the item from the array in an xml layout though. – Jorsher Nov 12 '10 at 3:31
Ah, sorry. I misunderstood. Well, does it have to be in the xml layout? If the title changes with user choice, why not just do it in the .java file. E.g. .setText(some_array[i]). – JJG Nov 12 '10 at 3:39
Because, the preferences layout is in xml. The java loads the menu with the items that are "enabled" in preferences. Typing up 17 checks for preferences, and the subsequent code to add it to the listview just seems redundant and sloppy to me. – Jorsher Nov 12 '10 at 4:22
feedback

Unfortunately:

  • It seems you can not reference a single item from an array in values/arrays.xml with XML. Of course you can in Java, but not XML. There's no information on doing so in the Android developer reference, and I could not find any anywhere else.

  • It seems you can't use an array as a key in the preferences layout. Each key has to be a single value with it's own key name.

What I want to accomplish: I want to be able to loop through the 17 preferences, check if the item is checked, and if it is, load the string from the string array for that preference name.

Here's the code I was hoping would complete this task:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());  
ArrayAdapter<String> itemsArrayList = new ArrayAdapter<String>(getBaseContext(),   android.R.layout.simple_list_item_1);  
String[] itemNames = getResources().getStringArray(R.array.itemNames_array);  


for (int i = 0; i < 16; i++) {  
    if (prefs.getBoolean("itemKey[i]", true)) {  
        itemsArrayList.add(itemNames[i]);  
    }  
} 

What I did:

  • I set a single string for each of the items, and referenced the single strings in the . I use the single string reference for the preferences layout checkbox titles, and the array for my loop.

  • To loop through the preferences, I just named the keys like key1, key2, key3, etc. Since you reference a key with a string, you have the option to "build" the key name at runtime.

Here's the new code:

for (int i = 0; i < 16; i++) {  
        if (prefs.getBoolean("itemKey" + String.valueOf(i), true)) {  
        itemsArrayList.add(itemNames[i]);  
    }  
}
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.