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);
Related
so as my question says, I am in a situation where I need to get ten different strings using getResources() and then compare them to a string the user enters.
This is how I'm doing it currently:
if(string == getResources().getString(R.string.x))
Since this code is going to be repeated ten times for ten different strings in R.java, I'm wondering if there is going to be a loss in performance by calling getResources() so many times.
Is there an easier solution, like creating an object from getResources() and then getting one string at a time from that object?
Sorry if I'm blabbering, I'm new to this.
Thanks!
Then you can do something like this:
Resources res = yourContext.getResources();
//use this res variable instead of getResources() function a small example
Bitmap bitmap = BitmapFactory.decodeResource(res,R.id.ic_launcher);
Another approach is to add the strings you're getting as a string-array
<string-array name="string_array_name">
<item>text_string</item>
<item>text_string</item>
</string-array>
and get your array:
String[] myArr = getResources().getStringArray(R.array.string_array_name);
and loop over them .. you'll call getResources() once ...
But anyways resources object is a shared object throughout the application and you're merely calling it's getter so no performance isuue there
you can create an array in string.xml
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="item_array">
<item>item 1</item>
<item>item 2</item>
<item>item 3</item>
<item>item 4</item>
...
</string-array>
</resources>
in your java code you access this as
String[] mTestArray = getResources().getStringArray(R.array. item_array);
To avoid the getResources() repeated call, create before a variable of type Resources, and use it instead of the call, as follows:
Resources res = getResources();
if (string == res.getString(R.string.x);
In this way, you can also pass the variable as a parameter to other methods/functions, having only one call to getResources() in all your code.
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
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. ;)
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.
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.