How to get the count of array from resources file? - android

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

Related

How can i create nested array in android studio Strings.xml

<resources>
<string name="app_name">UnConv</string>
<string-array name="mainunit">
<item>Area</item>
<item>Pressure</item>
<item>Speed</item>
<item>Volume</item>
</string-array>
</resources>
i want to add some other values to the above items like a subgroup. Example for area i want yard, acre etc how can i achieve that?
Unfortunately, There is no direct way to save two dimensional string-array in Android resource file. I provide 2 methods for replacement.
1. Save the data as Json string showing in the following code:
<string name="mainunit">{"Area":["yard","acre"],"Pressure":[],"Speed":[],"Volume":[]}</string>
Then parse Json String to Java/Kotlin Object.
2. Save subgroup value splitting with ",", and get value by position, but it has drawback because it ignored the key name.
<string-array name="mainunit">
<item>yard,acre</item>
<item>Pressure</item>
<item>Speed</item>
<item>Volume</item>
</string-array>

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);

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.

Reference a specific item in a <string-array> from a preferences XML?

Assuming I have a string array defined as:
<string-array name="settings">
<item>not very unique</item>
<item>not very unique</item>
<item>slightly more unique</item>
</string-array>
Is there a way to reference an item, by its index in the array, from a preferences XML?
I know how to reference the entire array:
android:entries="#array/settings"
But is there a way to reference only one item from the array?
Well no, there is no way, but you could do something like this:
<string name="mystring_1">awesome string is awesome</string>
<string name="mystring_2">even more awesome string</string>
<string-array name="mystrings">
<item>#string/mystring_1</item>
<item>#string/mystring_2</item>
</string-array>
So you basically reference another string in your resources, that is referenced in your string array. ;)

Reading Statics from an Array Named in an Array?

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.

Categories

Resources