Android: preferences not being stored automatically

Posted by Vitaly on Stack Overflow See other posts from Stack Overflow or by Vitaly
Published on 2011-02-26T22:35:44Z Indexed on 2011/02/26 23:25 UTC
Read the original article Hit count: 453

I'm trying to use preference screen. I'm following all steps from online tutorial (once I couldn't get it working, I found other tutorials, and steps seem to be fine). I get to preferences screen, edit values, return to calling activity (via hardware return button). In DDMS perspective FileExplorer shows package_name_preferences.xml file with preferences that should be stored. It contains:

 <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
 <map>
 <string name="false">kg</string>
 </map>

while I expect (data line only shown).

<string name="weight">kg</string>

Also, if I go change only 1 preference, the same value changes, not a new row is created. I'm just tempted to write my own preference classes that would store data in files or DB, but I know that preferences should work, it just doesn't save properly my stuff.

Edit

Tutorials used: Main Tutorial - Was using this as a base, simplified, as I needed only 3 listPreferences so far.

Another One - Used this one back when first installed android, so referred to this one for its section on preferences

Code: (Screen loads, so I'm not showing Manifest)

public class MyPrefs extends PreferenceActivity {
   @Override
   public void onCreate(Bundle bundle) {
       super.onCreate(bundle);
       addPreferencesFromResource(R.xml.my_prefs);
   }
}

my_prefs.xml

<PreferenceScreen
  xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="Value Settings">
        <ListPreference android:title="Distance"
                        android:summary="Metric (Kilometer) vs Imperial (Imperial)"
                        android:defaultValue="km"
                        android:key="@+id/distanceMesurement"
                        android:entries="@array/distance" 
                        android:entryValues="@array/distance_values"/>
        <ListPreference android:title="Weight"
                        android:summary="Metric (Kilogram) vs Imperial (Pound)" 
                        android:defaultValue="kg"
                        android:key="@+id/weightMesurement"
                        android:entries="@array/weight"
                        android:entryValues="@array/weight_values"/>
    </PreferenceCategory>
</PreferenceScreen>

calling MyPrefs from MainScreen

Intent i = new Intent(MainScreen.this, MyPrefs.class);
startActivity(i);

arrays.xml

<resources>
<string-array name="weight">
    <item name="kg">Kilogram (kg)</item>
    <item name="lb">Pound (lb)</item>
</string-array>
<string-array name="weight_values">
    <item name="kg">kg</item>
    <item name="lb">lb</item>
</string-array>
<string-array name="distance">
    <item name="km">Kilometer (km)</item>
    <item name="mi">Mile (mi)</item>
</string-array>
<string-array name="distance_values">
    <item name="km">km</item>
    <item name="mi">mi</item>
</string-array>
</resources>

© Stack Overflow or respective owner

Related posts about android

Related posts about android-preferences