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();
}
Related
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);
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.
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
I am reading shared preferences like
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
and then with preferences.getString("list_of_text_modes", "0") i can get value of any shared preferences. In my example (0 or 1)
Is it possible to get title too, not just value?
For example. I am using ListPreference.
<ListPreference
android:entries="#array/list_of_text_modes"
android:entryValues="#array/list_of_text_mode_values"
android:key="list_of_text_modes"
android:summary=""
android:title="#string/title_language_mode"
android:defaultValue="default" />
<string-array name="list_of_text_modes">
<item>Default</item>
<item>Settings</item>
</string-array>
<string-array name="list_of_text_mode_values">
<item>0</item>
<item>1</item>
now i get "0" if i choose "Default". Can i read somehow title "Default". Or with preferences i can read only values?
And what if I don't have 0 and 1. What if i save as "text1" and "tetx2". Can i read by key, value pair?
You can extract titles from your resources, if you have title index. Try this code:
CharSequence[] titles = context.getResources().getTextArray(R.array.list_of_text_modes);
String myTitle = titles[titleIndex];
You can only get the value. If you have a look at the actual shared preferences file which you can pull from the DDMS -> Data - Data -> Package name. You will see only the value and the key is stored and not the title.
But it is not really a problem because you have it already in your array.
Good luck
I was trying to figure this out as well. Too late for the original question, but I came up with a variation on Hit's answer. For arrays where both the title and value are strings it's not simple to find the index.
<string-array name="sound_keys">
<item>Gong1</item>
<item>Gong2</item>
</string-array>
<string-array name="sound_values">
<item>gonghi</item>
<item>gongmid</item>
</string-array>
But since the value is known you can search the value array and get the index that way, and use that to get the title from it's array. I have a function that does something like this:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String prefValue = sp.getString("sound_values", "some default");
// the arrays used by the ListPreference
CharSequence[] keys = getApplicationContext().getResources().getTextArray(R.array.sound_keys);
CharSequence[] values = getApplicationContext().getResources().getTextArray(R.array.sound_values);
// loop and find index...
int len = values.length;
for (int i = 0; i < len; i++) {
if (values[i].equals(prefValue)) {
return (String) keys[i];
}
}
// if not found use some default value
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");