getIntArray returns 0 from my XML file - android

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);

Related

Array in XML: insert one array into another as a part

Preparing string resources for my Android application, I've faced such a problem. I need two string arrays with 12 and 13 items, and first 11 items are common for both arrays. It would be a good idea to create array of 11 common items, then create two needed arrays contained 11 items from the 'common' array plus 1 and 2 additional items. However, I cannot find a way to do it. It is possible to reference to some xml resource, but in my case it makes necessary to include each 11 common items separately.
Thanks in advance for any idea
In your XML, you can reference common array as item:
<resources>
<string-array name="common_array">
<item>name 1</item>
<item>name 2</item>
<item>name 3</item>
</string-array>
<string-array name="new_array">
<item>#array/common_array</item>
<item>name 4</item>
</string-array>
</resources>
Then in your java code, you add following function which will convert the resource items to list of strings:
List<String> getArrayItems(int resourceId, Resources resources) {
ArrayList<String> itemList = new ArrayList<>();
TypedArray typedArray = resources.obtainTypedArray(resourceId);
for (int i = 0; i < typedArray.length(); i++) {
int type = typedArray.getType(i);
if (type == TypedValue.TYPE_STRING) {
itemList.add(typedArray.getString(i));
} else if (type == TypedValue.TYPE_REFERENCE) {
int resIdOfArray = typedArray.getResourceId(i, 0);
List<String> itemsForGivenRes = Arrays.asList(resources.getStringArray(resIdOfArray));
itemList.addAll(itemsForGivenRes);
}
}
typedArray.recycle();
return itemList;
}
Finally, you can use that function to get list of items in new_array:
List<String> arrayItems = getArrayItems(R.array.new_array, getApplicationContext().getResources());
Log.d("MyAdapter", "onCreate: " + arrayItems.toString()); // should print MyAct: onCreate: [name 1, name 2, name 3, name 4]
Note that in my example, I have considered string arrays only. With small change, you can adapt function to different arrays as well.
I hope my answer helps.

Android - R.array.[variable] in a For loop?

I'm looking for option to access array of strings defined in strings.xml in loop.
Means, I have in my strings.xml - several arrays like:
<string-array name="ques1">
<item>bla bla</item>
<item>bla bla</item>
</string-array>
<string-array name="ques2">
<item>bla bla</item>
<item>bla bla</item>
</string-array>
How can i access in my code - the arrays of ques1, ques2...etc ? (for example - i have many questions in my application with its answers, so i want to access it in generic way).
String[] stringArray = context.getResources().getStringArray(R.array.ques1);
for(String s : stringArray) {
//do something with s
}
Edit ... I just realized you want to iterate not over the array, but over several arrays. That's also possible, with :
for(int i = 0; i<max_arrays; i++) {
int id = context.getResources().getIdentifier("ques"+i, "array",
context.getPackageName());
String[] stringArray = context.getResources().getStringArray(id);
for(String s : stringArray) {
//do something with s
}
}

android filling array with strings from string.xml

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

get list of the strings present in strings.xml file in Android

I want the list of strings present in the strings.xml file.
Does anyone knows how to get it??? One thing I found is it assigns the ids in sequential order inside R.java but how to get the starting id is not clear.
For Example I have 100 Strings in my strings.xml like below and I want to read in at a time not like giving getResources().getString(int id) for individual.
<string name="app_label">Calendar</string>
<string name="what_label">What</string>
<string name="where_label">Where</string>
<string name="when_label">When</string>
<string name="timezone_label">Time zone</string>
<string name="attendees_label">Guests</string>
<string name="today">Today</string>
<string name="tomorrow">Tomorrow</string>
You can declare your strings in res\values\strings.xml file like this.
<string-array name="vehiclescategory_array">
<item>Cars</item>
<item>Bikes</item>
<item>RVs</item>
<item>Trucks</item>
<item>Other Vehicles</item>
</string-array>
In your activity class, you can access them like the following.
String[] categories;
categories=getResources().getStringArray(R.array.vehiclescategory_array);
In the above list, whatever sequence you declare, the same way it is assigned to the array in your activity. Suppose Cars will be assigned to categories[0]. Hope this helps.
Field[] fields = R.string.class.getDeclaredFields(); // or Field[] fields = R.string.class.getFields();
String str = "";
for (int i =0; i < fields.length; i++) {
int resId = getResources().getIdentifier(fields[i].getName(), "string", getPackageName());
str += fields[i].getName() + " = ";
if (resId != 0) {
str += getResources().getString(resId);
}
str += "\n";
}
You will get all codes of strings with its values in "str" variable.
If you want to access all the Strings from the strings.xml file you could use reflection on the R.string class. An example can be found in this answer, you'll just need to replace drawables with strings.
You could declare an integer array with an entry for each string. I did this for an array of colors once, so I imagine it works for strings as well.
res/values/arrays.xml
<integer-array name="app_strings">
<item>#string/app_label</item>
<item>#string/what_label</item>
<item>#string/where_label</item>
<item>#string/when_label</item>
<item>#string/timezone_label</item>
<item>#string/attendees_label</item>
<item>#string/today</item>
<item>#string/tomorrow</item>
</integer-array>
Then in your code, you would loop over the array and use each value as the argument for getString().
int[] stringIds = getResources().getIntArray(R.array.app_strings);
String[] strings = new String[stringIds.length];
for (int i = 0; i < stringIds.length; i++) {
strings[i] = getString(stringIds[i]);
}
The problem is you have to manually update your arrays.xml whenever you modify your string resources, so it's certainly not ideal.
String[] categories = getResources().getStringArray(R.array.stars_array);
List<String> stringList = new ArrayList<>(Arrays.asList(categories));
use this simple one

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");

Categories

Resources