sharedpreferences - Create multiple preferences android -


i´m getting desperate when trying create second preference activity of app. have implemented sharedpreferences, works pretty good, trying create second preference activity work on specific activity, cannot make work properly. happening that, no matter color select, giving me "default value" = 1, if preference file checking not exist. here code of preferenceactivity:

public class preferenciasgrafica extends preferenceactivity {   @override protected void oncreate(bundle savedinstancestate) {        super.oncreate(savedinstancestate);        addpreferencesfromresource(r.xml.preferencias_grafica);     }  public void onbackpressed(){     //preferencias              //preferencemanager.getdefaultsharedpreferences(this);//carga archivo preferencias             sharedpreferences appprefs2 =this.getpreferences(mode_private);             int colore=integer.parseint(appprefs2.getstring("color","1"));//pasa samples las prefer. elegidas             //startactivity(new intent(this, main.class));             switch(colore){             case 1:                 colore=color.red;             break;             case 2:                 colore=color.blue;             break;             case 3:                 colore=color.green;             break;             }             grafica grafica=new grafica(colore);             //grafica.color=color;              startactivity(new intent(this, grafica.class));              finish();    } } 

this xml of preference file:

    <preferencescreen    xmlns:android="http://schemas.android.com/apk/res/android"    android:key="preferencias_principal" >     <listpreference     android:key="color"     android:title="color de la gráfica"     android:summary="selecciona el color del gráfico"     android:entries="@array/colores"    android:entryvalues="@array/valores"     android:defaultvalue="0"/>     </preferencescreen> 

and array file:

 <resources>   <string-array name="colores">       <item>rojo</item>       <item>azul</item>       <item>verde</item>   </string-array>   <string-array name="valores">       <item>1</item>       <item>2</item>       <item>3</item>    </string-array> </resources> 

does know doing wrong here? i´m getting totally wasted this!

edit: problem solved!! change , make this:

sharedpreferences appprefs2 =preferencemanager.getdefaultsharedpreferences(this);

try using

  preferencemanager.getdefaultsharedpreferences(getapplicationcontext()); 

when you're acquiring sharedpreferences.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -