Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

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.

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top