Where to declare lots of constant objects? - android

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 !

Related

Compare strings independently of current language

i'm having a hard time trying to understand how to deal with language - and trying to explain myself in english.
For example, take a look at this
<resources>
<string name="temperature_celsius">Celsius</string>
<string name="temperature_fahrenheit">Fahrenheit</string>
<string name="temperature_kelvin">Kelvin</string>
<string-array name="temperature">
<item>#string/temperature_celsius</item>
<item>#string/temperature_fahrenheit</item>
<item>#string/temperature_kelvin</item>
</string-array>
</resources>
Now, imagine that i have another xml file where Celsius, Fahrenheit and Kelvin are written differently in another language.
//name of temperature units in another galaxy
<resources>
<string name="temperature_celsius">Celsioso</string>
<string name="temperature_fahrenheit">Fahrenheitzkeum</string>
<string name="temperature_kelvin">Kalvon</string>
<string-array name="temperature">
<item>#string/temperature_celsius</item>
<item>#string/temperature_fahrenheit</item>
<item>#string/temperature_kelvin</item>
</string-array>
Now, i want my alien user to see the units in its own language, so i need to compare them taken that into account. Is there an efficient way in which i could do this instead of hardcoding the value of each string item? I tried using a switch but it says constant expression required, i think it works if i use if-else, but it looks ugly:
//convert from celsius(baseUnit) to something else(endUnit)
//baseUnit and endUnit are strings taken from two autocompletetextviews
if(baseUnit.equals(mContext.getResources().getString(R.string.temperature_celsius))) {
convertFromCelsius(endUnit, inputValue, mContext);
I'm handling the UI in my main activity, the job of converting each unit is done in other java classes (that's why i'm using a mContext).
Thanks, and sorry for my bad english.

Best way to do TextView.setText() for lots of strings

I have an app that needs to check a random number, and then print out from the string.xml file. Here is the code I currently have (it does carry on but it's just declaring the randNum and rand):
randNum = rand.nextInt(59);
switch (randNum){
case 1:
random.setText(R.string.f_vocab1);
break;
This (needs to) goes on for another 59 cases and strings. I've just started Android development so I don't have a clue for a better way to do this. Can someone please tell me a better way to do this?
You could define a String Array in your strings.xml like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array
name="string_array_name">
<item>first_string</item>
<item>second_string</item>
</string-array>
</resources>
In your code you can access this array with this line:
String[] stringArray = getResources().getStringArray(R.array.string_array_name);
Now generate your random number and get the string by index:
int randNum = rand.nextInt(59);
random.setText(stringArray[randNum];
Note: To get a random number isn't what you want I think, because you will get negative values. You have to deal with this properly in your switch-block or with the array approach I'm suggesting.

How can define two dimension string array in android resource?

I wish to fill in a spinner control with MsgName field, and get MsgValue when I select a item of the spinner.
so I write the following code.but I don't think it's a good code, is there the better code?
Do I need to define two dimension string array for my app? How can I do? Thanks!
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<string-arrayname="MsgName">
<item>Inbox</item>
<item>Sent</item>
</string-array>
<string-arrayname="MsgValue">
<item>content://sms/inbox</item>
<item>content://sms/sent</item>
</string-array>
</resources>
I would want to explicitly associate each name with it's value, instead of just having them be in the same position in two separate arrays.
There's at least a couple of ways to accomplish this.
Use some sort of separator to concatenate the name and the value in the same item:
e.g.:
<string-array name="message">
<item>Inbox|content://sms/inbox</item>
<item>Sent|content://sms/sent</item>
</string-array>
Create a custom xml resource, e.g. res/values/message-list.xml:
<messages>
<message name="Inbox">content://inbox</message>
<message name="Sent">content://sent</message>
</messages>
Of course, either way, you're going to have process the contents and create a SpinnerAdapter - in the first case, you'll have to split the entries, and in the second case, you'll have to parse the xml.

Are "strings.xml" string arrays always parsed/deserialized in the same order?

Can I count on string arrays within the "strings.xml" resource file to be parsed/deserialized in the same order every time?
If anyone can cite any documentation that clearly spells out this guarantee, I'd appreciate it. Or, at the very least, offer a significant amount of experience with this topic.
Also, is this a best practice or am I missing a simpler solution?
Note: This will be a small list, so I'm not looking to implement a more complicated database or custom XML solution unless I absolutely have to.
<!--KEYS (ALWAYS CORRESPONDS TO LIST BELOW ??)-->
<string-array name="keys">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
<!--VALUES (ALWAYS CORRESPONDS TO LIST ABOVE ??)-->
<string-array name="values">
<item>one</item>
<item>two</item>
<item>three</item>
</string-array>
Yes, as far as I'm aware you can assume that the order of items will be the same each time, meaning you can safely define key/value pairs using separately xml-declared arrays. Have a look at the API demos (e.g. the arrays.xml file) and you'll see that Google uses the same methodoly to specify static key/value pairs. More specifically, you'll be able to deduce this from entries_list_preference and entryvalues_list_preference. Actually, if you think about it: it would hardly make sense to offer entries and entryValues attributes for pointing to static resources for e.g. a ListPreference if their order wouldn't be guaranteed.
Addendum: Multi-dimensional arrays in xml are not supported. You can however write your own xml parser to handle those cases, which actually isn't as hard as it may sound. It would probably take you more time though than simply defining two one-dimensional arrays.

Array defining in Android Application

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...

Categories

Resources