samedi 2 mai 2015

Saving int variable using SharedPreferences (not working)

I created an app that on button click increases its integer value from 0 by 1. (it's displayed in a textView) What I'm trying to do is to save that int variable so the modified value will remain after restarting the app. I was trying to use these methods, putting them inside onClick(increase) method as well in onCreate but nothing seems to work:

    SharedPreferences mPrefs = getSharedPreferences("label", 0);
    Integer var = mPrefs.getInt("var", variable);

    SharedPreferences.Editor mEditor = mPrefs.edit();
    mEditor.putInt("tag", variable).commit();

and also this:

    SharedPreferences sharedPref= getSharedPreferences("mypref", 0);
    SharedPreferences.Editor editor= sharedPref.edit();
    editor.putInt("name", variable);
    editor.commit();

    getSharedPreferences("name", variable);

After exiting an app and lauching it the value is reseted to 0

Here's full code:

import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;


public class TestApp extends ActionBarActivity {


 public int variable = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_app);



    TextView statnumber = (TextView) findViewById(R.id.number);
    statnumber.setText(String.valueOf(variable));



    SharedPreferences mPrefs = getSharedPreferences("var", 0);
    Integer var = mPrefs.getInt("var", variable);

    SharedPreferences.Editor mEditor = mPrefs.edit();
    mEditor.putInt("var", variable).commit();






}

public void increase(View view){
    TextView statnumber = (TextView) findViewById(R.id.number);
    statnumber.setText(String.valueOf(variable = variable + 1));
}


}

Here's what logcat says (probably not important..):

05-02 22:23:53.010  10827-10827/com.rpd.testapp D/OpenGLRenderer﹕ Enabling debug mode 0
05-02 22:24:56.200  11058-11058/com.asd.testapp D/dalvikvm﹕ Late-enabling CheckJNI
05-02 22:24:56.440  11058-11058/com.asd.testapp D/ActivityThread﹕ setTargetHeapUtilization:0.25
05-02 22:24:56.450  11058-11058/com.asd.testapp D/ActivityThread﹕ setTargetHeapIdealFree:8388608
05-02 22:24:56.450  11058-11058/com.asd.testapp D/ActivityThread﹕ setTargetHeapConcurrentStart:2097152
05-02 22:24:56.730  11058-11058/com.asd.testapp I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
05-02 22:24:56.810  11058-11058/com.asd.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 12212: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
05-02 22:24:56.810  11058-11058/com.asd.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
05-02 22:24:56.810  11058-11058/com.asd.testapp I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
05-02 22:24:56.810  11058-11058/com.asd.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 12218: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
05-02 22:24:56.810  11058-11058/com.asd.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
05-02 22:24:56.810  11058-11058/com.asd.testapp I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
05-02 22:24:56.810  11058-11058/com.asd.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 9783: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
05-02 22:24:56.810  11058-11058/com.asd.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
05-02 22:24:56.900  11058-11058/com.asd.testapp I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
05-02 22:24:56.910  11058-11058/com.asd.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 12215: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
05-02 22:24:56.910  11058-11058/com.asd.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007
05-02 22:24:56.930  11058-11058/com.asd.testapp I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
05-02 22:24:56.930  11058-11058/com.asd.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 396: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
05-02 22:24:56.930  11058-11058/com.asd.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
05-02 22:24:56.930  11058-11058/com.asd.testapp I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
05-02 22:24:56.930  11058-11058/com.asd.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 418: Landroid/content/res/TypedArray;.getType (I)I
05-02 22:24:56.930  11058-11058/com.asd.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
05-02 22:24:56.930  11058-11058/com.asd.testapp I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
05-02 22:24:56.930  11058-11058/com.asd.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 359: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
05-02 22:24:56.930  11058-11058/com.asd.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
05-02 22:24:56.940  11058-11058/com.asd.testapp I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
05-02 22:24:56.940  11058-11058/com.asd.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 361: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
05-02 22:24:56.940  11058-11058/com.asd.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
05-02 22:24:57.170  11058-11058/com.asd.testapp D/libEGL﹕ loaded /system/lib/egl/libEGL_adreno200.so
05-02 22:24:57.190  11058-11058/com.asd.testapp D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_adreno200.so
05-02 22:24:57.190  11058-11058/com.asd.testapp D/libEGL﹕ loaded /system/lib/egl/libGLESv2_adreno200.so
05-02 22:24:57.200  11058-11058/com.asd.testapp I/Adreno200-EGL﹕ <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_REL_2.0.3.1_RB1.04.01.01.45.000_msm8625_JB_REL_2.0.3.1_Merge_release_AU (Merge)

It is my first time when I'm creating an app that creates cache so I don't fully understand how it works. Any help appreciated.

Aucun commentaire:

Enregistrer un commentaire