How to access a string from a string Array? - android

I'm brand new to android developing, so I'm sorry if this is a terrible rookie mistake.
That being said, I know in java, if I have a string array named example, I could just do
example[0] to access the first string. I'm basically making a quiz app where it will put answers in a random order, but I don't believe I can just accesss the string array in string resources (my values/strings.xml) with this method? Thanks.

From here
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);

You can retrieve Strings from Resources this way:
String string = getString(R.string.hello);
More details here.

Related

Can you select a string resource with a name like

I am thinking of creating a small dynamic layout using strings.xml as a base.
I know how to select single resources but is there a way of selecting ALL resources with a name like "Intro_"
A bit like a Select where like query.
So if i had some strings like;
Intro_Welcome
Intro_FirstParagraph
SecondScreen_Welcome
List of strings mList = select all string resources with a name like "Intro_"
mList would now contain the string of Intro_Welcome and Intro_FirstParagraph
Any help is usefull
Thank you
You can't do this like you described. The string resources name that you define in your xml file are mapped into int values. So, when you use they in your code, you use by the int value created.
You may consider using a modified version of this class, that can get the string resource id within the string text.

How to get a value in a string list?

I have a list containing the following:
String list[]={"Team 1,Apple,Mango,Kiwi","Team 2,Mango,Kiwi,Pineapple","Team 3,Kiwi,Pineapple,Apple"};
my question is, how do you know what each team has? also how do you get the team name?
i would like to have the output:
Team 1,Team 3(Apple,Kiwi)
Team 1,Team 2(Mango,Kiwi)
Team 2,Team 3(Pineapple,Kiwi)
can someone tell me what can be done or what to do?
In order to get the values of the first string in a list of strings you can split the string by "," and put it in a list like following: String s1 [] = list[0].split(","); and then s1[0] is Team1 s1[1] is Apple etc..
You can make use of StringTokenizer to accomplish your task.
http://developer.android.com/reference/java/util/StringTokenizer.html

How to get tags from textfield?

I have a textfield in android called TagField.
I have an array of Strings called ListOfTags.
I enter a few words separated by spaces into Tagfield and require the function to extract the tags and store it into ListOfTags.
The algorithm I have in mind is:-
Store the string from the field into a String object
To first locate positions of all the spaces and store it in an array
Use the positions to extract sub strings from the main string
Store each sub string into ListOFStrings
My Question is, Is there a more efficient method?
Since I am using a lot of arrays
This can be achieved as-
String str = tagField.getText().toString();//get string from the editText.
String[] strArray = str.split(" ");//regular expression which separates the tags.
List<String> listOfTags = Arrays.asList(strArray);
Hope this helps.

Getting string array from String values folder?

In my Values folder for my project, I have a string array named currency_avatar. I want to be able to use this array of strings in my activity.
I know that "R.array.currency_array" this line of code is legal, however I do not know how to actually get this to refer to a local String array in my own activity.
So, how do I refer this currency_array located in my values folder of res to a local String array variable in my activity?
Sorry for such a simple question. :)
String[] currencies = getResources().getStringArray(R.array.currency_array)
This article will help you in following problems you can face.

I have an array contains string values, in that same name i have image in drawable.so i want to add in integer array how to do that?

I have an array contains string values, in that same name i have image in drawable.so i want to add in integer array how to do that? ex string newarray[]={"jj","kk","ll"};
i want to access R.drawable.jj ,R.drwable.kk,R.drawable.ll
To access the drawables you need an int. Instead of saving the names in a String array, save the IDs in an int array:
int[] drawables = {R.drawable.jj, R.drawable.kk, R.drawable.ll};
Then all you need to do to access them is call:
getResources().getDrawable(drawables[0]);
You could try to get the identifier by name:
Resources res = getResources();
res.getDrawable(res.getIdentifier(myStringArray[0], "drawable", myPackage));
A similar question was already answered: get resource id by passing name as a parameter in android

Categories

Resources