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.
Related
Just a quickie,
i have an xml resource in res/values/integers.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="UserBases">
<item>2</item>
<item>8</item>
<item>10</item>
<item>16</item>
</integer-array>
</resources>
and ive tried several things to access it:
int[] bases = R.array.UserBases;
this just returns and int reference to UserBases not the array itself
int[] bases = Resources.getSystem().getIntArray(R.array.UserBases);
and this throws an exception back at me telling me the int reference R.array.UserBases points to nothing
what is the best way to access this array, push it into a nice base-type int[] and then possibly push any modifications back into the xml resource.
I've checked the android documentation but I haven't found anything terribly fruitful.
You need to use Resources to get the int array; however you're using the system resources, which only includes the standard Android resources (e.g., those accessible via android.R.array.*). To get your own resources, you need to access the Resources via one of your Contexts.
For example, all Activities are Contexts, so in an Activity you can do this:
Resources r = getResources();
int[] bases = r.getIntArray(R.array.UserBases);
This is why it's often useful to pass around Context; you'll need it to get a hold of your application's Resources.
get an Array from array.xml in resources of android project can be accessed.
from array.xml
<string-array name="weather_values">
<item>sunny</item>
<item>cloudy</item>
<item>rainy</item>
</string-array>
in Activity
String[] stringsArray = getApplicationContext().getResources().getStringArray(R.array.weather_values);
in Fragment
String[] stringsArray = getContext().getResources().getStringArray(R.array.weather_values);
for output in log
System.out.println("Array Values " + Arrays.toString(stringsArray));
output is
I/System.out: Array Values [sunny, cloudy, rainy]
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
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);
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'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.