2015-05-03 58 views
0

我創建了一個應用程序,按鈕單擊將其整數值從0增加1(它顯示在一個textView中) 我想要做的就是保存該int變量所以重新啓動應用程序後修改的值將保持不變。 我試圖使用這些方法,把它們內部的onClick(增加)的方法,以及在的onCreate,但似乎沒有任何工作:使用SharedPreferences保存int變量(不工作)

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

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

而且這樣的:

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

getSharedPreferences("name", variable); 

退出應用後和空空它的價值reseted 0

這裏是全碼:

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)); 
    } 
} 

這裏是logcat的說(也許並不重要。):

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) 

這是我第一次當我創建創建緩存,所以我不完全瞭解它是如何工作的應用程序。任何幫助讚賞。

回答

0

在你TestApp活動var總是返回0.這低於存儲在MyPrefsFile文件中的所有你的價值觀,關鍵= silentMode

如何從SharedPreferences值

 public static final String PREFS_NAME = "MyPrefsFile"; 
     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); //PREFS_NAME is file name not your key name 
     boolean silent = settings.getBoolean("silentMode", false); 
     setSilent(silent); 

如何存儲值

 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
     SharedPreferences.Editor editor = settings.edit(); 
     editor.putBoolean("silentMode", mSilentMode); 

     // Commit the edits! 
     editor.commit(); 

你真的需要訪問Official Doc - Storage Options Shared Preferences

0

我沒有看到onCreate裏面的任何地方variable被更改。因此,您存儲的值已被初始化 - 0。這就是爲什麼您始終從首選項中獲得0的原因。

0

試試這個 SharedPreferences myPreference = getSharedPreferences(「sharedpref_name」,MODE_PRIVATE); //將數據寫入SharedPreferences Editor editor = myPreference.edit(); editor.putString(「var_name」,「variable_value」); editor.commit();

0
SharedPreferences myPreference =getSharedPreferences("sharedpref_name", MODE_PRIVATE); 

    // Writing data to SharedPreferences 
    Editor editor = myPreference.edit(); 
    editor.putString("var_name", "variable_value"); 
    editor.commit();