Android: how to parameterize which resource to use at runtime? [duplicate] - android

This question already has answers here:
Access resource string by string name in array
(2 answers)
Closed 7 years ago.
Think about a list of parameters and their respective intervals of discrete integer values:
a[1-N], b[1-M], c[1-K], d[1-J]
a, b, c, d are the variables while between square brackets there are intervals of their possible values.
At runtime if they are
a=1, b=2, c=3, d=5
then I'd like to get the resource with
name = R.string.string_1_2_3_5
Is it possible?
I wouldn't want to make a series of cascade switches for each variable to finally pick a resource. I know this could work but is there another way?

You can use Java reflection like in here.
If you need to retrieve strings like that more than once, in order to get fast access, you should first construct a hashtable with the field names of R.string as keys (maybe when you launch the app).

Related

create object from random String

I have a string like this "*POS0210X/Hello from ECR/T64999"
In the string above X/ is and indicator of a type and /T a field. I want to create objects from those strings. Given that i know all the special combinations above, whats the best way to do it in kotlin?
PS there can be up to 10 different fields and the combination of them generate different objects

Generate only not generated random numbers in a certain range android studio? [duplicate]

This question already has answers here:
How to generate a random permutation in Java?
(3 answers)
Closed 1 year ago.
Hi in my Android application I need to generate all numbers(not together but when I call the function say generateAnyRandom()) in a certain range.
For example, say my range is 1 to 1000 and when I call generateAnyRandom() it should give a random number within the range. In this way, if again I call the function it should give me another random number which must be different from the existing one. And more importantly, I will call the function maximum of 1000 times as the range is 1 to 1000 and it should give me always a number that was not generated yet. That means I want to generate all the numbers from 1 to 1000 not in one shot but only when I call it.
Please help. It is a critical question for me?
This is a known problem.
Put the numbers 1..1000 into some container; an array, list, or whatever.
Shuffle the contents of the container into random order.
Retrieve the numbers from the shuffled container in order.
As you correctly point out, this will only work 1000 times before the container is exhausted. You should pick a type of container that allows you to easily perform a random shuffle. Check the various built-in functions for the different container types.

How to store array of integer in sharedPreferences [duplicate]

This question already has answers here:
Save ArrayList to SharedPreferences
(38 answers)
Closed 6 years ago.
I have an array integer in android studio like this { 0 , 0 , 0 , 1 , 1 , 0 , 1}
how can I store and recall it in shared preferences
There is no way you can store an array as it is.
Either you can convert it to JSON String and store it as a string.
Or you can store each element of the array individually with an ID like this.
Build a string out of it. Preferably using StringBuilder.
And, when reading, just split() on commas and then user Integer.parseInt() on all elements of the array.
Should be faster than the json route and does not require any extra dependency.

How to extract the string variable in android? [duplicate]

This question already has an answer here:
How to extract this string variable in android?
(1 answer)
Closed 9 years ago.
String test=["1","Low-level programming language",true]
Here i want to extract this string value and as i need to get only second value like "Low-level programming language".How to get this value using string functions in android?
Per your comment, I'm assuming that you have a single string that contains the entire text (including the brackets). In general, splitting comma-separated values is a fairly tricky process. For your specific string, though, it's kind of easy:
String test = "[\"1\",\"Low-level programming language\",true]";
String[] pieces = test.split(",");
String middle = pieces[1];
// now strip out the quotes:
middle = middle.substring(1, middle.length() - 1);
In general, you might want to look at using a general CSV parser like Apache Commons CSV or openCSV.
Alternatively, if this is JSON data (which looks more likely than CSV), take a look at using one of the Java JSON libraries listed here (scroll down the page to see the list).

How to dynamically define name of R.drawable [duplicate]

This question already has answers here:
How to get the image from drawable folder in android?
(4 answers)
Closed 9 years ago.
Is there a way to generate R.drawable.... name on the fly? I want to set a background image with a dynamically generated name ( which is already usable as R.drawable.example_graphic)
So i want to find a way to assign a String to $ABC -> btn.setBackgroundImage(R.drawable.$ABC);
I don't want to create a new drawable, i want to use the existing one.
Yes. Drawables are constants at compile time: you know exactly what exists. So, in your Application object, create a map:
public static final Map<String, Integer> NAMED_RESOURCES;
static {
Map<String, Integer> m = new HashMap<String, Integer>();
m.put(KEY1, R.drawable.ABC);
m.put(KEY2, R.drawable.DEF);
// ...
NAMED_RESOURCES = Collections.unmodifiableMap(m);
}
now you can:
btn.setBackgroundImage(Application.NAMED_RESOURCES.get($ABC));
i have found the solution, as #CommonsWare has shared.
getContext().getResources().getIdentifier("flags_"+country, "drawable","mypackage")
However, it may require cacheing as it uses reflection.
So, creating a static HashMap to keep the ResourceName and it's result from getIdentifier function (integer) seems to be a good idea; further usages for the same ResourceName will just get the value from HashMap instead of using the reflection again.

Categories

Resources