Question

I'm using a simple set & get class so that I can retrieve a variable throughout my app but I need to store this class for later use so I want to use shared preferences to store it but I am not sure how to implement this correctly. I tried to implementing the shared preferences within the setter method and there are no errors but this crashes when I run.

Here is the class

import android.app.Activity;
import android.content.SharedPreferences;
import android.widget.EditText;



public class Passcode extends Activity {

    private String pin;
    public static final String PREFS_PASS = "MyPrefsFile";
    EditText txtNewPin = (EditText)findViewById(R.id.txt_EnterNewPin);
    SharedPreferences Pincode = getSharedPreferences(PREFS_PASS,0);
    SharedPreferences.Editor editor = Pincode.edit();

    public String getPin() {
        Pincode.getString(pin,"0000");
        return pin;
    }

    public void setPin(String pin) {

        editor.putString(pin, txtNewPin.getText().toString());
        editor.commit();

    }


}
Was it helpful?

Solution

use below method for get and set variable in shared preference.

 /**
 * This method is used to set shared preferences
 * @param context Application context
 * @param key shared object key
 * @param value shared object value
 */
public static void setPreferences(Context context, String key, String value) {
    SharedPreferences appSharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(context);
    Editor prefsEditor = appSharedPrefs.edit();
    prefsEditor.putString(key, value);
    prefsEditor.commit();
}

/**
 * This method is used to get shared object
 * @param context Application context
 * @param key shared object key
 * @return return value, for default "" asign.
 */
public static String getPreferences(Context context, String key) {

    SharedPreferences appSharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(context);

    String json = appSharedPrefs.getString(key, "");
    if (TextUtils.isEmpty(json)) {
        return null;
    }
    return json;
}

It is worked for me.

OTHER TIPS

where is the oncreate method ? suppose u want to manipulate a boolean value in shared pref :

      SharedPreferences  SharedPref_;
       Editor _prefsEditor;
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     // to get
    SharedPref_ = this.getSharedPreferences("MainPref", MODE_WORLD_READABLE);
            downloaded = SharedPref_.getBoolean("Downloaded", false) ;

    // to set a shared pref : 
    _prefsEditor = SharedPref_.edit(); 
            downloaded =  true;
            _prefsEditor.putBoolean("Downloaded",true);
            _prefsEditor.commit();


}

I don't believe you can initialize your EditText outside of a method because the layout may not be set. For that reason, I'll put it inside the methods. The same applies to the SharedPreferences. Just put it all in the methods:

private String pin;
public static final String PREFS_PASS = "MyPrefsFile";

public String getPin() {
    EditText txtNewPin = (EditText)findViewById(R.id.txt_EnterNewPin);
    SharedPreferences Pincode = getSharedPreferences(PREFS_PASS,0);
    Pincode.getString(pin,"0000");
    return pin;
}

public void setPin(String pin) {
    SharedPreferences Pincode = getSharedPreferences(PREFS_PASS,0);
    SharedPreferences.Editor editor = Pincode.edit();
    editor.putString(pin, txtNewPin.getText().toString());
    editor.commit();
}

However I also see one more problem: String pin is never initialized. Make sure that is has a value before you use it in Pincode.getString(pin, "0000");

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top