android filling array with strings from string.xml - android

what I have learned so far:
in strings.xml I have bunch of
<string name="q1">what is the name of blah blah</string>
<string name="q2">what is the name of blah blah</string>
<string name="q3">what is the name of blah blah</string>
<string name="q4">what is the name of blah blah</string>
and in the main class i have to create an array of question objects to access them
throughout the main class like
private Question[] mQuestionList = new Question[]{
new Question(R.string.q1, true),
new Question(R.string.q2, false),
new Question(R.string.q3, true),
new ...
}
What im curious about is that if I have 200 questions, i know i have to write each of them out in stirng.xml file, however i think 200 "new Question(R.string.qx, true/false)" statements in the main class is not the right way. i believe that there is some way of filling the mQuestionList array in much easier way. If there is what is it?

You can put this in values/arrays.xml
<string-array name="array_name">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
</string-array>
then do
String[] questionStrings = getResources().getStringArray(R.arrays.array_name);
mQuestionList = new Question[questionStrings.length];
for (int i = 0; i < questionStrings.length; i++) {
mQuestionList[i] = new Question( questionStrings[i], ...
If the second parameter, the boolean one, is different for each question, store them an a new array in values/arrays.xml

Related

getIntArray returns 0 from my XML file

Working on an SQLite database. I'm importing data from my XML file.
The problem I got is when I try to read my int array, I only get 0 as values.
Part of my XML file :
<integer-array name="difficulties">
<item>8</item>
<item>12</item>
<item>4</item>
<item>5</item>
<item>7</item>
</integer-array>
How I am getting the array :
public int[] getDifficulties() {
Resources res = this.getResources();
return res.getIntArray(R.array.words);
}
How I am testing the array :
int[] test = getDifficulties();
for(int i=0; i<test.length; i++) {
Log.i("MESSAGE", Integer.toString(test[i]));
}
Thanks for help!
You are not accessing your difficulties array correctly. Replace return res.getIntArray(R.array.words); with return res.getIntArray(R.array.difficulties);

Using one getStringArray call to different String arrays

In the strings.xml file have few String arrays, for example:
<string-array name="T1"> //demolitionHammers
<item>Makita HM1200K</item>
<item>Makita HM1202C</item>
<item>Bosch GSH 5E</item>
<item>Bosch GBH 11DE</item>
</string-array>
<string-array name="T2"> //diamondDrills
<item>Makita DBM080</item>
<item>Bronco BDD150A</item>
</string-array>
<string-array name="T3"> //groundDenser
<item>Shatal PC2016</item>
</string-array>
In the Java file I have a string variable with the name of the wanted array, for example(the wanted array will determine from other activity and will be different each time):
String strName = "T2";
The problem is that I cant use now the following call to get the specific String-array:
String[] selectedSubList = res.getStringArray(R.array.strName);
How can this can be done?
You can use the getIdentifier() method
Resources res = context.getResources();
int resId = res.getIdentifier(strName, "array",context.getPackageName());
String[] selectedSubList = res.getStringArray(resId);

How to store integer value in arrays.xml file

in arrays.xml i declared 1 array like this:
<string-array name="music_code">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
and in other Activity extends from PreferenceActivity, i declared:
int value = PreferenceManager.getDefaultSharedPreferences(context)
.getInt("music_code, 1);
But there is error when running. so why?
I also changed above xml code like this:
<array name="music_code">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</array>
help me please! thank
You need to get the array before you can retrieve values from it. I have an example below, also maybe ContentValues would be more suitable for your needs?
String[] array = getResources().getStringArray(R.array.music_code);
int value = Integer.parseInt(array[INDEX]);
You can also use ContentValues if you need to index values, or a database if you need more control over retrieving and saving them. ContentValues is as such:
int value = 5;
ContentValues cv = new ContentValues();
cv.put("KEY", value);
int val = cv.get("KEY");

Android random non repeating string

I'm making a bingo type game. I have a 5x5 grid of imagebuttons, each with their own textview. When the app starts or is reset, I want each textview to display a random string without any one string being displayed twice during a game. I currently have the strings in a resource array, with 127 items:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="tile_text">
<item>String 1</item>
<item>String 2</item>
<item>String 3</item>
...all the way to String 127
</string-array>
</resources>
And to display a random string on each textview:
public String[] myString;
Resources res = getResources();
myString = res.getStringArray(R.array.tile_text);
Random random = new Random(System.currentTimeMillis());
int[] textViews = {
//I have all my textviews added to this array
};
for(int v : textViews) {
TextView tv = (TextView)findViewById(v);
tv.setText(myString[random.nextInt(myString.length)]);
}
The above works well, but even with 200 strings in the array to choose from, some items still show up twice. Is there a way I can get the array to shuffle and not pick the same string twice for per game? I have searched and I find info on random strings, but nothing about non repeating random strings, so apologies if this is a duplicate question.
I'd keep a list of the strings you'e already added and then keep picking new random strings until you find one that's not already in your list.
Something like this:
Vector<String> alreadyUsed = new Vector<String>();
for(int v : textViews) {
TextView tv = (TextView)findViewById(v);
String nextString;
do {
nextString = myString[random.nextInt(myString.length)];
} while (alreadyUsed.contains(nextString));
alreadyUsed.add(nextString);
tv.setText(nextString);
}

how we can get the arraylist(both string and integer) from the resources xml

Hi want to get the arraylist from the resources.xml is there any code for this.please give me some suggestions.Thanks in advance
Use Arrays.asList :
String[] myResArray = getResources().getStringArray(R.array.my_array);
List<String> myResArrayList = Arrays.asList(myResArray);
This list will be immutable, so if you want a mutable list, just do :
List<String> myResMutableList = new ArrayList<String>(myResArrayList);
I have done this way:
string.xml:
Define String Array:
<string-array name="my_string_array">
<item>Test 1</item>
<item>Test 2</item>
<item>Test 3</item>
<item>Test 4</item>
<item>Test 5</item>
<item>Test 6</item>
</string-array>
In Activity.class / Fragment.class:
List<String> myArrayList = Arrays.asList(getResources().getStringArray(R.array.my_string_array));
Done
A modification to Klaus' answer, to get an ArrayList<String>:
ArrayList<String> myResArrayList = new ArrayList<String>();
Resources res = getResources();
Collections.addAll(myResArrayList, res.getStringArray(R.array.myResArray));
Not entirely sure what you want, but this might be worth a read: http://developer.android.com/guide/topics/resources/string-resource.html
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
</resources>
How to retrieve the array in your application with code:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);
Price_array in strings.xml is assigned to Arraylist
private ArrayList<String> price_unit;
price_unit= new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.Price_array)));

Categories

Resources