Array defining in Android Application - android

I want to use the concept of array in my Android Application, I don't know how to do that actually.
So could anybody please help me how to do that on demand.

I guess you are talking about arrays in Android through the res folder.
Create an array.xml inside the /res/values folder with something like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="names_list">
<item>John</item>
<item>Peter</item>
<item>Charles</item>
</string-array>
</resources>
You can get that array on your Activity by doing:
getResources().getStringArray(R.array.names_list);

There are alot of different "array" types in java... there are actual arrays like Thorsten showed you and then there are lists, collections and hashes. Take you pick. :) A great place to start learning more about Java is the docs.
http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/
http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/

This defines an array of 5 strings:
String[] stringArray = new String[5];
However, I can not imagine that this is really what you're talking about...
CLARIFICATION
If you actually don't know what an array is, then my reply will give you a hint. In case you're talking about something else, this reply should indicate that you're not giving enough detail. You might as well be talking about this...

Related

Android: Organizing Strings and String Arrays in res/values

I am working on a project that includes a lot of strings and string arrays. I would like to put them into created folders inside res/values, but I get errors when I try to do this. Either getRecources() does not recognize the new folder or the xml attributes cannot link together. I know this is a noob-ish question, but thanks for the help!
Unfortunately, you can't create any subfolders in your values folder. But you have two instruments to control the hierarchy.
String arrays are declared in the following way:
<string-array name="arr_name">
<item>Text</item>
<item>Another text</item>
</string-array>
You can access them through R.array.arr_name.
Prefixes are kind of obvious, but since you mentioned that you are a novice, it's worth mentioning. I usually prefix all of my strings depending on how they are used. For example, btn_ for the text used on buttons, dialog_ for strings used in dialogs and so on. This way autocomplete in the IDE also works much better too.
Also you can split your declarations into different files, but this doesn't have any impact at all on the way you access them, so I don't know if this can help you.
You can define array of strings using following way. Later you can access it in code with R.values.langs
<string-array name="langs">
<item>бг</item>
<item>en</item>
<item>ру</item>
</string-array>
To organise my res folder I use defined xml files not sub-folders.
Basic Example:
- if you have Strings for your Login Page put them in login_strings.xml
- if you have Strings for your Options Page put them in options_strings.xml
etc.
Hope this helps.

Android - Search XML (String array) file for items

I want to make something that searches through an XML file, to see if a string, entered by the user, exists. My XML file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources><string-array name="dict_nl_array" ><item>ADHD</item><item>ADSL</item><item>AMvB</item><item>AOV</item><item>AOW</item><item>uniteit</item></string-array></resources>
The only difference is that my XML is about 500 times as long as this.
I tried loading the arraylist (xml) directly, but then I get this error:
Failed adding to JNI local ref table (has 512 entries)
Which seems logical, because its a gigantic list of items.
So, I was thinking, can I search through that XML file, without having to load it completely?
Ofcourse, other suggestions to do this are welcome! (If possible, with an example, I'd greatly appreciate that!)
Late answer, but it might help anyone else with this problem!
Based on this question, you can avoid this error by encoding the resource as a string, rather than a string-array.
<string name="array">ADHD#ADSL#AOV#AOW</string>
The # character separates the entries. To speed up the reformatting, just Find/Replace </item><item> with #. Then you can parse the string into an array like so:
myStringArray = getResources().getString(R.id.array).split("#");
Hope this helps!

Where to declare lots of constant objects?

This might look like a stupid question but I actually have objects - let's call the object "Cocktail" - that contains a certain amount of fields such as cocktail name, list of ingredients, recipe, etc...
So basically what I would like to do is have my cocktails list available when my application needs it, but I do not really know how to store them. I thought of declaring all cocktails in Android string arrays such as following :
<
?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array
name="first_cocktail_name">
<item>ingredient1</item>
<item>ingredient2</item>
<item>ingredient3</item>
<item>recipe</item>
</string-array>
<string-array
name="second_cocktail_name">
<item>ingredient1</item>
<item>ingredient2</item>
<item>ingredient3</item>
<item>recipe</item>
</string-array>
</resources>
And so on, but problem is I don't have the possibility of declaring item key so retrieve all the info will be a bit tedious, for example if the number of ingredients vary between different cocktails index retrieving will not work...
I'm not a big fan of hard-coding all my cocktail objects directly in code when app starts either so I don't really know what to do with that...
Any idea ?
Thanks !

Android set String value in String.xml

can anybody help me ?? I want to add String value through coding in string.xml
I am doing this.
String name = getResources().getString(R.string.name);
if(name.lenght() < 1 ){
// getResources().setString(R.string.name);??????????????????????
}
My string.xml is
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="name"></string>
</resources>
does anybody know how i can add value of name in string.xml though coding.
Thank you!!
The resources are pretty much set in stone, so you can't modify them at runtime. If you need to store some new strings, use SharedPreferences or SQLite.
I don't think you want to do that. If you are trying to store a value in a persistent way, take a look at SharedPrefences. Google has a good introduction to it here.
It is not possible to modify the resources of an APK during runtime.
You can't edit those resources directly. You might want to look into sharedpreferences http://developer.android.com/reference/android/content/SharedPreferences.html or creating your own xml file.
You cant edit a resource or add a resource once the code is compiled. I dont know exactly what setResource does, but once your program is compiled, android builds the gen files which designate a certain amount of space for those variables, changing the variable once written would cause overflow or outofbounds errors with memory. If you want persistent values try using the SharedPrefs, SQL or even your own XML stored within the directory of the app, which you could set to only be readable by your app.

Dynamically referencing a String in Strings.xml in Android

Say I have the following my strings.xml file:
<string name="string0">Lorem</string>
<string name="string1">ipsum</string>
<string name="string2">dolor</string>
In my activity an ID is set based on the clicking of a button. If the top button is clicked the id is 0, middle is 1 and bottom button is 2.
What would the syntax look like for referencing one of the three strings?
I know R.string.string0 works but I want to do something equivalent to:
R.string["string"+currentID]
where I derive the string to use based on the ID.
Just not sure what the syntax would look like in Java/Android.
Thanks in advance,
Tony
Could you not just use a string array in your resources instead of separate string entries?
That's a bad approach. It's slow. It'd be better to have an internal integer array with all the R.string IDs.
If you really insist on using a string-based approach, use Resources.getIdentifier().

Categories

Resources