How to store integer value in arrays.xml file - android

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

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

getResourceEntryName() :can i manage to get "R.id.+getresourceentryname(R.string.myString)"?

im trying to resolve the following problem : i named my string resource like that in my xml string file :
<string name="array_nature">Nature</string>
<string name="array_architecture">Architecture</string>
<string name="array_arts">Art/Literature</string>
<string name="array_cinema">Cinema/TV/Radio</string>
<string name="array_geography">Geographie</string>
<string name="array_leisure">Loisirs</string>
<string name="array_music">Musique</string>
<string name="array_people">People</string>
<string name="array_sciences">Sciences</string>
<string name="array_technology">Technologie</string>
<string-array name="array_category">
<item>#string/array_nature</item>
<item>#string/array_architecture</item>
<item>#string/array_arts</item>
<item>#string/array_cinema</item>
<item>#string/array_geography</item>
<item>#string/array_leisure</item>
<item>#string/array_music</item>
<item>#string/array_people</item>
<item>#string/array_sciences</item>
<item>#string/array_technology</item>
</string-array>
i also have string-arrays corresponding to those two strings :
<string-array name="array_nature">
<item>Species</item>
<item>Plant</item>
<item>Animal</item>
<item>CelestialBody</item>
<item>Asteroid</item>
<item>Galaxy</item>
<item>Planet</item>
<item>Satellite</item>
<item>Star</item>
</string-array>
<string-array name="array_architecture">
<item>AmusementParkAttraction</item>
<item>Building</item>
<item>MilitaryStructure</item>
<item>Infrastructure</item>
<item>Airport</item>
<item>RouteOfTransportation</item>
<item>Bridge</item>
<item>HistoricPlace</item>
<item>Monument</item>
<item>Park</item>
<item>Zoo</item>
</string-array>
as you can see, the attribute name of my string (name = "array_nature") is exactly the same as the string-array name.
My goal is to avoid an annoying if, else if, else if ... code in my activity... So is that possible to get Something like that in my java code?
int myStringArray = R.id.+getResourceEntryName(R.string.array_nature);
and i stuck in this method
public void getchoicearray() {
Spinner sp = ((Spinner)findViewById(R.id.spinner_category));
int id = getResources().getIdentifier(getResources().getResourceEntryName(sp.getSelectedItem()./*how to get the entry
name of selected spinner item*/), "array", getPackageName());
choiceArray = id;
ArrayAdapter a = ArrayAdapter.createFromResource(this, choiceArray, android.R.layout.simple_spinner_item);
((Spinner) findViewById(R.id.spinner_choice)).setAdapter(a);
}
or any other value corresponding to a spinner choice...
EDIT : added xml spinner content
you can use public int getIdentifier (String name, String defType, String defPackage) to retrive the id at runtime.
name is the name of the resource. The one you specified in the the name attribute in the xml
defType is the type of the resource. E.g. drawable. In your case you have to use "array", because the resource you want is an array.
defPackage, is the package name of your app. You can retrieve it using getPackageName().
If you want to retrieve the id of array_nature, you will have:
int myStringArray = getResources().getIdentifier("array_nature", "array", getPackageName())
getIdentifier returns 0 it the resource can't be found. So you should always check if the return value is grater than 0
Resources res = getResources();
String[] planets = res.getStringArray(R.array.array_nature);
Do the above one in strings.xml.

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

How to get value of shared preference

I want to retrieve the "entries" not the "entryValue" from a shared preference. I am using this and it gets the entryValue:
String notifyInterval = PreferenceManager.getDefaultSharedPreferences(mActivity).getString(ACCUWX.Preferences.PREF_NOTIFY_INTERVAL, null);
Here is the XML and array files:
<ListPreference
android:key="pref_temp_notifications"
android:title="#string/notifications"
android:entries="#array/pref_temp_notifications"
android:entryValues="#array/pref_temp_notifications_values"
android:dialogTitle="#string/notifications"
android:defaultValue="2"/>
<string-array name="pref_temp_notifications">
<item>#string/my_current_location</item>
<item>#string/home_location</item>
<item>#string/off</item>
</string-array>
<string-array name="pref_temp_notifications_values">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
So I'd like to retrieve the string value, not the numeric. The numeric is what I get returned and assigned to my variable notifyInterval. How do I grab the text?
You need to use getAll () method which returns Map, from that map, get KeySet which returns entries of shared preference.
Map<String, ?> test = getAll ();
Set keySet = test.keySet();
Iterator<String> keySetIter = keySet .iterator();
while (keySetIter.hasNext()) {
String keyEntry= keySetIter.next();
}

Categories

Resources