Reading Statics from an Array Named in an Array? - android

I have a load of hierarchal static configuration data that I want to read, for example;
<string-array name="Root">
<item>Array1</item>
<item>Array2</item>
<item>Array3</item>
</string-array>
<string-array name="Array1">
<item>Array1 Data1</item>
<item>Array1 Data2</item>
<item>Array1 SubArray1</item>
<item>Array1 SubArray2</item>
<item>Array1 SubArray3</item>
</string-array>
<string-array name="Array1 SubArray1">
<item>Array1 SubArray1 Data1</item>
<item>Array1 SubArray1 Data2</item>
</string-array>
<string-array name="Array2">
<item>Array2 Data1</item>
<item>Array2 Data2</item>
<item>Array2 SubArray1</item>
<item>Array2 SubArray2</item>
<item>Array2 SubArray3</item>
</string-array>
<string-array name="Array1 SubArray3">
<item>Array2 SubArray3 Data1</item>
<item>Array2 SubArray3 Data2</item>
</string-array>
Putting in 1,1 would give me;
Array1 Data1, Array1 Data2, Array1 SubArray1 Data1, Array1 SubArray1 Data2
While putting in 2,3 would give me;
Array2 Data1, Array2 Data2, Array2 SubArray3 Data1, Array2 SubArray3 Data2
What I want to do is something like;
Resources res = getResources();
String[] RootArray = res.getStringArray(R.array.Root);
String[] Array = res.getStringArray(R.array.RootArray[1]);
String[] SubArray = res.getStringArray(R.array.Array[1]);
Is there a way to get this sort of thing to work?
Note: The Array sizes are not necessarily fixed.
Thanks in advance.
Mark

There is no way of implementing this exact behavior using named arrays. However, it's perfectly possible to do so using a custom XML parser, which can be easily written from the XML libraries included with Android.
The problem is that when you reference R, you are making a reference to an auto-generated, static integer which the android compiler creates to refer to a resource. When you actually compile your application, the java compiler replaces the reference to the number with the actual number. Thus,
res.getStringArray(R.array.MyArray);
really becomes something like
res.getStringArray(0x12345);
which is obviously not an actual "array".
In order to implement your desired functionality, create an XML file in /res/xml/config.xml:
<my-config>
<array name="Array1">
<data>Item 1</data>
<data>Item 2</data>
<array name="SubArray 1">
<data>Item 4</item>
</array>
</array>
</my-config>
In your application, you can load the XML file by calling
XmlResourceParser XMLparser = getResources().getXml(R.xml.config);
and then use the methods of the XMLResourceParser to pull the appropriate values.

Related

How to get the count of array from resources file?

I have multiple arrays in resources like this:
<resources>
<string-array name="myArray1">
<item>String1</item>
<item>String2</item>
<item>String3</item>
</string-array>
<string-array name="myArray2">
<item>String1</item>
<item>String2</item>
<item>String3</item>
</string-array>
<string-array name="myArray3">
<item>String1</item>
<item>String2</item>
<item>String3</item>
</string-array>
</resources>
Now how to get the count of these array dynamically?currently I have 3 arrays.
I can get the particular array items like this
String[] resourceString =getResources().getStringArray(R.array.myArray);
But how to get the count?
int length =getResources().getStringArray(R.array.myArray).length;
The XML file is not for counting the arrays inside it. It is for storing information that doesn't belong in your code in a static way.
This way you can alter menu's, lists dynamically.
What you can do is instead of creating multiple arrays, is store it ** multidimensional**. link
Now you can count the arrays like user1969053 is suggesting

Android - String Array with switch statement

I am looking for a guide as to how i can implement switch that would get values from a string array. Basically, i have a spinner with all the items as stated in my array, so i want to implement a switch so that whenever i click on an item, it will trigger an event, for example, going to another activity.
<string-array name="location1">
<item>string a</item>
<item>string a</item>
<item>string b</item>
<item>string c</item>
<item>string d</item>
</string-array>
So, if i have a string array like this, how should I implement my switch statement?
If array is:
<string-array name="cars_array">
<item>Audi</item>
<item>Ferrari</item>
</string-array>
You can access them like this:
Resources res = getResources();
String[] cars = res.getStringArray(R.array.cars_array);
then you can access them individually as cars[0], cars[1] etc
If you want to access the same string resource as part of an array and individually as well, then you can do this:
<string name="audi">Audi</string>
<string name="ferrari">Ferrari</string>
<string-array name="cars_array">
<item>#string/audi</item>
<item>#string/ferrari</item>
</string-array>
Then you can simply say "#string/audi" to get it individually as well as you can use the array name "#string/cars" to use the string array as a whole.
I think this resource on Android Developers site might help you : String Resources
It will get you something like that :
Resources res = getResources();
String[] strArray = res.getStringArray(R.array.location1);

How to get String-Array referenced by another String-Array?

For example if I have the following String_Array
<string-array name="recipe_ingredients">
<item>#array/m1_ingredients</item>
<item>#array/m2_ingredients</item>
</string-array>
How can I access #array/m1_ingredients where it is another String_Array
EDIT :
No, every item of a StringArray must always be a String, of course. So it can't be another StringArray
check the Documentation on resources, scroll to StringArray
name
String. A name for the array. This name will be used as the resource ID to reference the array.
here's an eg
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
</resources>
This application code retrieves a string array:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);

ExpandableList array resource implementation questions

I am not exactly clear on how to implement ExpandableList as array resource instead of hard-coded array (by replacing an existing hard-coded array with XML layout). Here is my strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">adaptor_ExpandableList1</string>
<string name="expandable_list_sample_action">Sample action</string>
<string-array name="Groups">
<item>People Names</item>
<item>Dog Names</item>
<item>Cat Names</item>
<item>Fish Names</item>
</string-array>
<string-array name="People Names">
<item>Arnold</item>
<item>Barry</item>
<item>Chuck</item>
<item>David</item>
</string-array>
<string-array name="Dog Names">
<item>Ace</item>
<item>Bandit</item>
<item>Cha-Cha</item>
<item>Deuce</item>
</string-array>
<string-array name="Cat Names">
<item>Fluffy</item>
<item>Snuggles</item>
</string-array>
<string-array name="Fish Names">
<item>Goldy</item>
<item>Bubbles</item>
</string-array>
</resources>
Here is what I changed in the Java file:
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// Sample data set. children[i] contains the children (String[]) for groups[i].
private String[] groups = getResources().getStringArray(R.array.Groups);
private String[][] children = {getResources().getStringArray(R.array.People Names),
getResources().getStringArray(R.array.Dog Names),
getResources().getStringArray(R.array.Cat Names),
getResources().getStringArray(R.array.Fish Names)
};
Of course R.array.xxx Names does not work. So how can I correct the problem when there are statements in R.java such as this: public static final int Cat Names=0x7f040003;.
There must be a rule for this but I don't know it yet.
Browsing through numerous examples, I think I also need to have group and child layout files and perhaps a main layout file (I think I know how to implement them if I need to). Is this correct?
Thanks in advance for your help!
you have space in the name of string array
remove space from "people names" and other string array names.

Android arrays - using string pattern to refernce arrays for use

I'm pretty new to Android, but a seasoned PHP dev, so some concepts I know how to do in PHP I'm struggling with in Android dev.
What I'm trying to do is use a string-array to define a textual pattern that I can swap out with a random value from another string array.
I know how to randomly choose an item from a string-array, so no problems there. But this is what I want to do:
<resources>
<string-array name="myPattern">
<item>myValues1</item>
<item>myValues1 of myValues2</item>
</string-array>
<string-array name="myValues1">
<item>string a</item>
<item>string b</item>
<item>string c</item>
<item>string d</item>
</string-array>
<string-array name="myValues2">
<item>string 1</item>
<item>string 2</item>
<item>string 3</item>
<item>string 4</item>
</string-array>
</resources>
The logic in my code is randomly choose a string from the pattern array. Then, swap out any instance of "myValues1" in that string with a random value from the string-array myValues1, and swap "myValues2" with a random item from myValues2 array.
Is this possible with arrays in Android, or should I be using some code to create the structures?
Thanks in advance.
Would this work for you?
class <yourActivity> extends Activity{
private final Random rand = new Random();
private String getRandom(int resourceId){
String [] vals = getResources().getStringArray(resourceId);
return vals[rand.nextInt(vals.length - 1)];
}
//your method somewhere...
void someMethod(){
swapOutMyValues1(getRandom(R.array.myValues1));
swapOutMyValues2(getRandom(R.array.myValues2));
}
}
Based on this page in the dev guide, you should be able to access the String arrays in your code like so:
Resources res = getResources();
String[] myValues1 = res.getStringArray(R.array.myValues1);
You'll want to do this in code because the resources you define in XML files are there to be used by the apps you write, so the idea is that you'll write code to make changes in some way.

Categories

Resources