I'm having a problem using strings stored in my strings.xml, I have a wide list of strings stored there. They are very useful for me because I'm using them to translate my program. However, now I want to choose between those strings dynamically and I don't know how to do it. It will be easier to understand with an example. Let's assume that I have the following strings:
<string name="red">Red</string>
<string name="blue">Blue</string>
<string name="green">Green</string>
<string name="yellow">Yellow</string>
And now let's assume that I have a function that passes me a string with a color, for example "yellow". Now I only have a solution for this, to make a very huge switch (very very huge, because I have lots of strings), I think that there must be an option to transform the output of my function into the right parameter.
I mean, if I have a function that returns me "yellow", and I want to use R.strings.yellow, there must be a link between them. I don't know if maybe you could use any kind of reflection to achieve this.
Can you help me?
There's a way 10 times faster than regular android method "getIdentifier" to get the value from not only string but also drawable or any other resource existing in the R file in a very simple manner using reflection as follows:
try {
//Get the ID
Field resourceField = R.string.class.getDeclaredField("yourResourceName");
//Here we are getting the String id in R file...But you can change to R.drawable or any other resource you want...
int resourceId = resourceField.getInt(resourceField);
//Here you can use it as usual
String yourString = context.getString(resourceId);
} catch (Exception e) {
e.printStackTrace();
}
Hope this helps.
Regards!
Use a two-step process to find the id to load. First use Resources.getIdentifier(), for example:
int id = getResources().getIdentifier("yellow", "string", getPackageName());
Then, after checking the id is not zero (which indicates it could not find the resource), use the id to get a string like normal:
String colour = getString(id);
String mystring = getResources().getString(R.string.yellow);
Related
I have a class called GamesData that has strings and getters and setters for this strings.
I download strings from a json. One of these strings is an URL with an image. I download the images but on a small size, because of their URL. I need to download it on a bigger size. For this, I need to remove this "small" string from the URL:
"home_team_logo": "https:\/\/URL\/images\/teams\/small\/olympique-marseille-890.png"
I have more than one URL coming from a big json object, all inside a json array, formated like the one above.
This is what I do to get the json.
arrayList.add(new GamesData(
gamesDataObject.getString(TAG_DATE),
gamesDataObject.getString(TAG_COMPETITION),
gamesDataObject.getString(TAG_HOME_TEAM),
gamesDataObject.getString(TAG_AWAY_TEAM),
gamesDataObject.getString(TAG_ID)
));
I need to remove from TAG_HOME_TEAM
public static final String TAG_HOME_TEAM= "home_team_logo";
, which is that URL above, only the "small" part, so the image downloaded will be the full size one.
In fact I will need this for all 3 tags: TAG_COMPETITION, TAG_HOME_TEAM, TAG_AWAY_TEAM.
How on earth do I do this? xD
You can use replace , use /small/ with / as replacement to avoid matching something like /othersmallteamname/
String s = "https:/URL/images/teams/small/olympique-marseille-890.png";
System.out.println(s.replace("/small/", "/"));
Output :
https:/URL/images/teams/olympique-marseille-890.png
I'm thinking something like this might work:
gamesDataObject.getString(TAG_HOME_TEAM).replaceAll("\/small\/","/");
This uses a regular expression to match all occurrences of the patern. A simplepr less error prone approach may be to use
gamesDataObject.getString(TAG_HOME_TEAM).replace("\/small\/","/");
Which should only replace the first occurrence.
I am trying to use a full id of a block in the getmaterial part of the code below. this does not work any way that i try.
I cannot find any documentation supporting this issue of handling an id which contains a 'colon :' .
Snip: (Example the 5758:6 below does not work and the string name neither.)
emerald.setIngredient('L', Material.getMaterial("5758:6"));
Material.getMaterial(406) //this is expecting an integer so i cannot give it two numbers
Material.getMaterial(406:1) //this fails as is expecting int
Assuming that emerald is a ShapedRecipe object (since you're using the setIngredient(char, Material) method), then you can also use the setIngredient(char, MaterialData) method instead. You could construct the MaterialData object you want using the (deprecated...) MaterialData(int, byte) constructor. Your new code would look like:
emerald.setIngredient('L', new MaterialData(5758, 6));
The colon in the "full id of a block" is just separating the "id" and "data" values. I think this will do what you're looking for, but if not, let me know so I can clarify.
I don't think you're supposed to be dealing with that number colon thing. Instead, if you want to get to, say, the BRICK material, use Material.BRICK or Material.valueOf("BRICK"). If you want to find the name of a Material m, use m.name() which returns a String.
This is probably a simple one and more Java related than grails but I'm a bit lost and not sure where to even start looking on this, I've googled about but am not really sure what I'm after, so would appreciate a pointer if possible please!
In the grails app I have a form which I save, all well and good. In the controller I can see the list of params it returns via a simple println and when I want to find a specific value currently I do a params.each and then compare the key to a pre defined string to find the one I want, my question is: -
Can I, and how would I, specifically say "get me the value of the parameter with the key "banana", rather than having to loop through the whole list to find it?
Also is there a way of creating a new set of secondary params, or just another plain old dictionary item (is that the right term?) where I use a regular expression to say "give me all the items whose key match the pattern "XYZ"?
It probably doesn't make much difference speed wise as the params are never that big but it'd be nice to make things more efficient where possible.
Any feedback much appreciated!
For a first question, to get 'banana' parameter you have to use:
params.banana
For second, find all with regexp:
def matched = params.findAll { it.key =~ /XYZ/ }
//or
Pattern p = ~/XYZ/
def matched = params.findAll { p.matcher(it.key).matches() }
There's a params object you can use. Eg with someurl.com?myparam=test you can access it with "params.myparam"
More information over here: http://grails.org/doc/2.2.x/ref/Controllers/params.html
I'm trying to change the values in my strings.xml using Java. It's to change certain labels in an app dynamically on certain events. Any help would be appreciated
You can't change values in strings.xml as Strings are immutable (unless you do some dynamic compilation). You can have enums of Strings that can change the answer depending on passed-in variables
I think the best solution for your need would be to define all possible strings within the XML File. Based on the event in your application you can then choose the correct string. If the event is something like a user input then the changing the XML file is not possible. But in this case it should be fine to change the String directly without using XML references.
You might want to take a look at Java String Formatter it simply let you format a string in your string.xml but not changing the whole String , as other answers said, you can't change the value of a String in strings.xml from your java code.
I am making an app that generates random numbers for android (you can find it hat https://github.com/JXPheonix/RNumGen ) and I need some way for an xml string to be dynamic (of sorts); it needs to change every time it is viewed.
I want the string to invoke a method that generates a random number. The string in my xml file goes something like this:
<string name="number">Your number is</string>
and I want after the is for a method in java to be called upon, which would subsequently change the ending of the string. Any way to do this (whether or not it involves actually editing the strings.xml file?)
You're going about it a round-about way. Instead of somehow making your string in your XML file dynamic, just use your string and append to it in code. Something like:
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText(getString(R.string.number) + yourRNGMethod());
Don't change the string, concatenate the constant string with your generated number in your code.
Alternatively, use the string as an input to String.format and use a placeholder for the number.
You don't want to use the strings.xml for strings which have changing values.
Its not clear what the scope for consumption of your random number is but you can always use the Math classes to generate a random number and then Integer.toString() the value to get the value as a string.