0

That is how I save values in SharedPreferences:

private void GuardarConfiguracionEnXML() {
        SharedPreferences prefs =getSharedPreferences("MisPreferencias",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();

        editor.putBoolean("ConfirmarPromocion", VarGlobales.ConfirmarPromocion);
        editor.putBoolean("NoPermitirCero", VarGlobales.NoPermitirCero);
        editor.putBoolean("ProponerCantidad", VarGlobales.ProponerCantidad);
        editor.putBoolean("ArticuloCodNum", VarGlobales.ArticuloCodNum);
        editor.putBoolean("GuardarRutas", VarGlobales.GuardarRutas);
        editor.putBoolean("IncluyeFabricante", VarGlobales.IncluyeFabricante);
        editor.putBoolean("CompararComienzo", VarGlobales.CompararComienzo);
        editor.putString("CadenaBusquedaArticulos", VarGlobales.CadenaBusquedaArticulos);
        editor.putString("SerieConfiguracion", VarGlobales.SerieConfiguracion);
        editor.putBoolean("MostrarPoblacion", VarGlobales.MostrarPoblacion);
        editor.putBoolean("RecogidaEnvases", VarGlobales.Recogidaenvases);
        editor.putBoolean("ConvertirACajas", VarGlobales.ConvertirACajas);
        editor.putBoolean("RepartoFinSemana", VarGlobales.RepartoFinSemana);
        if(VarGlobales.ModoBusArticulos == BuscarPorArticulo.CODIGO)
            editor.putInt("BusquedaDefectoArticulos", 3);
        else if(VarGlobales.ModoBusArticulos == BuscarPorArticulo.DESCRIPCIONC)
            editor.putInt("BusquedaDefectoArticulos", 1);
        else
            editor.putInt("BusquedaDefectoArticulos", 2);
        editor.putInt("DiasDiurno", VarGlobales.DiasDiurno);
        editor.putInt("DiasNocturno", VarGlobales.DiasNocturno);
        //editor.putBoolean("VentaSoloUnidades", VarGlobales.VentaSoloUnidades);
        editor.commit();

        Map<String, ?> testshared = prefs.getAll();
    }

testshared has all values, but when I start my apk i get 0 values:

SharedPreferences prefs = getSharedPreferences("MisPreferencias",Context.MODE_PRIVATE);
        Map<String, ?> testshared2 = prefs.getAll();

testshared2is empty... Why?

EDIT: Was an Eclipse bug, I've rebooted my computer and now it works... Thanks to everybody.

3 Answers3

0

Try this

change

SharedPreferences prefs =getSharedPreferences("MisPreferencias",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();

To this

SharedPreferences prefs =getSharedPreferences("MisPreferencias",Context.MODE_PRIVATE);
Editor editor = prefs.edit();
user3331142
  • 1,222
  • 1
  • 11
  • 22
0

Your "put/commit" code looks correct to me.

However, there are some pitfalls with getAll :

How to iterate through all keys of shared preferences?

I suggest you try retrieving an individual value (e.g. using getBoolean or getString) to determine whether this is the problem.

Community
  • 1
  • 1
IanB
  • 3,489
  • 1
  • 20
  • 24
  • I've used getAll while I was debugging, I just was using this to know which values had my sharedpreferences file. Was an Eclipse bug, I've rebooted my computer and now it works... – Jose Antonio Benitez Montero Jun 25 '14 at 16:29
  • The point I was making was that the getAll looked suspect and my suggestion was to use getBoolean or getString instead. – IanB Jun 25 '14 at 16:40
0

I use sharedPreferences based on my activities. You can use "getApplicationContext()" if you are out of activity or fragment class, you can use "getActivity()" in fragment class or "this" in activity class.

To put data:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
Editor prefsEditor = prefs.edit();
refsEditor.putString(Key1, Value1);
refsEditor.putString(Key2, Value2);
prefsEditor.commit();

to retrieve data:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String str = prefs.getString(Key1, "defValue");
João Marcos
  • 3,872
  • 1
  • 19
  • 14