2

I use Preference Manager to save some Integers and boolean values. I have created a SettingsPreferences class :

public class SettingsPreferences {

    private Context mContext;

    public SettingsPreferences(Context context) {
        this.mContext = context;
    }

    public boolean isNull() {
        if (PreferenceManager.getDefaultSharedPreferences(mContext) == null) {
            return true;
        } else {
            return false;
        }
    }

    public void setBoolean(String name, boolean value) {
        PreferenceManager.getDefaultSharedPreferences(mContext).edit()
                .putBoolean(name, value).apply();
    }

    public void setInt(String name, int value) {
        PreferenceManager.getDefaultSharedPreferences(mContext).edit()
                .putInt(name, value).apply();
    }

    public boolean getBoolean(String name, boolean defaultValue) {
        return PreferenceManager.getDefaultSharedPreferences(mContext)
                .getBoolean(name, defaultValue);
    }

    public int getInt(String name, int defaultValue) {
        return PreferenceManager.getDefaultSharedPreferences(mContext).getInt(
                name, defaultValue);
    }}

In the onCreate method in the Main class I add the default values :

mSettingsPreferences = new SettingsPreferences(getApplicationContext());
if(mSettingsPreferences.isNull() == true) {
    mSettingsPreferences.setBoolean("MAX", 1);
    mSettingsPreferences.setBoolean("PROGRESS", 1);
}

In a fragment class I need to load that data and displayed it in a progress bar. Here is the code :

Thread t = new Thread() {
   public void run() {
     try {
       sleep(100);
       SettingsPreferences sett = new SettingsPreferences(mContext);
       mProgBar.setMax(sett.getInt("MAX", 2));
       mProgBar.setProgress(sett.getInt("PROGRESS", 1));

     } catch (InterruptedException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
   }
};
t.start();

The context is defined I checked him, the progressbar also, but every time it loads me the default values. What is the problem?

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
mvd3
  • 99
  • 1
  • 5
  • in your setXXX methods, after edit, you must commi (http://developer.android.com/reference/android/content/SharedPreferences.html#edit%28%29) – Luca Sepe Mar 22 '14 at 10:07
  • http://stackoverflow.com/a/10186553/2015318 – StarsSky Mar 22 '14 at 10:08
  • In addition to the above comment yu are using setBoolean in one call yet you are using getInt in another for the same key! Don't initialise the values just pass in a default! Inconsistencies like this are the road to nightmares – jamesc Mar 22 '14 at 10:14

3 Answers3

0

Use this:

For initialize the preference use this:

SharedPreferences preferences = PreferenceManager
        .getDefaultSharedPreferences(context);
String string = preferences.getString(key, defValue);

Now For Storind Data:

Editor editor = preferences.edit();
editor.putString(key,value);
editor.commit();

For more information see this tutorial.

Hope you understand.

Kishan Dhamat
  • 3,746
  • 2
  • 26
  • 36
  • The problem isn't in the SettingsPreference class methods because I use the same methods in other classes and it returns me the saved value. – mvd3 Mar 22 '14 at 13:35
0

You have to call comit() of Editor to apply change

tauitdnmd
  • 369
  • 2
  • 9
0

I suppose your isNull() always returns false, because :

PreferenceManager.getDefaultSharedPreferences(mContext)

will not return null, at least I never seen such case. This way you never initialize your MAX and PROGRESS values, and in the end you read defaults.

You can use SharedPreferences.contains method to check if MAX exists, and if not then initialize it.

marcinj
  • 48,511
  • 9
  • 79
  • 100