0

Alright so, I am trying to save a set of three phone numbers to a shared pref. When I hit save it goes through all the code and the toast and the end pops up but the numbers that should have been saved do not show instead it showed the default text I input for each number. Interested conundrum. Anyway here in the code chunk I am working with.

        saveNums.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String num1String;
            num1String = num1.getText().toString();
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString(constants.num1, num1String);
            editor.commit();

            String num2String;
            num2String = num2.getText().toString();
            SharedPreferences.Editor editor2 = prefs.edit();
            editor2.putString(constants.num2, num2String);
            editor2.commit();

            String num3String;
            num3String = num3.getText().toString();
            SharedPreferences.Editor editor3 = prefs.edit();
            editor3.putString(constants.num3, num3String);
            editor3.commit();

            Toast.makeText(c, "Contact's have been saved: " + "\n" + constants.num1 + "\n" + constants.num2 + "\n" + constants.num3, Toast.LENGTH_LONG)
                    .show();

        }
    });

And here is the shared pref file:

import android.content.SharedPreferences;

public class constants {
    public static String PREF_NAME = "sharedString";
    public static String num1 = "num1";
    public static String num2 = "num2";
    public static String num3 = "num3";
    SharedPreferences prefs;
}

No errors are coming up when I hit that saveNums button. All the same the numbers are not being saved either. Any ideas on how to fix this would be awesome.

HpTerm
  • 8,151
  • 12
  • 51
  • 67
CodeMonkeyAlx
  • 813
  • 4
  • 16
  • 32
  • check the exact way to retrieve values and apply sharedPref in [this SO ques](http://stackoverflow.com/questions/16194567/android-sharedpreferences-how-to-save-a-simple-int-variable) , ''constants.num1'' wont work – Pararth Jul 02 '14 at 11:41
  • 2
    how do you instantiate `prefs`? – Ivo Jul 02 '14 at 11:42
  • As @IvoBeckers says, you have to instantiate SharedPreferences using getSharedPreferences somewhere in your code. Where do you do that ? – HpTerm Jul 02 '14 at 11:47

3 Answers3

1

It seems to me like you do not have understood the concept of the SharedPreferences and key/value pairs in general.

SharedPreferences can be used to store a value, associated with a key of free choice.

Example:

String key = "key";

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Context);
Editor e = prefs.edit();

e.putString(key, "stringtosave");
e.commit();

This means, that the value "stringtosave" is now stored in the SharedPreferences, associated with the key "key". When retrieving the value, you need to provide the key again.

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Context);
String retrieved = prefs.getString(key); // retrieved will then contain "stringtosave"

What you are doing in your Toast message is showing the keys associated with the values you saved. Those keys will never change and obviously always have the same value.

If you want to show the actual saved value in the Toast message, you need to retrieve it from the SharedPreferences or use the value that you just stored (e.g. "num1String").

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
1

Add this code in your helper class

public static final String MY_ST_PREFS = "what ever you want";

public static void savePreferences(Context context, String key, String value) {

    SharedPreferences sharedPreferences = context.getSharedPreferences(
            MY_ST_PREFS, 0);
    Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
}

public static String getPreferences(Context context, String key) {

    String prefsStr = "";

    SharedPreferences sharedPreferences = context.getSharedPreferences(
            MY_ST_PREFS, Context.MODE_PRIVATE);
    prefsStr = sharedPreferences.getString(key, "");

    return prefsStr;

}

And in your onButton click just add this for saving data in Shared Preferences

yourhelperclass.savePreferences(context,"yourkey",yourstringvalue);

And for getting Shared preferences data just use this code

String myString = yourhelperclass.getPreferences(context,"yourkey");

I hope this will work...

Android_dev
  • 320
  • 1
  • 7
  • 26
  • Perfect and I understand how it works. Essentially its taking all the major work out of doing it all in file and putting it into a few methods. Perfect - thank you! – CodeMonkeyAlx Jul 02 '14 at 12:02
0

To store in shared pref

public synchronized void storeInPref(Context context, String key, String value) {
        SharedPreferences preferences = context.getSharedPreferences(
                PREFERENCE_NAME, Context.MODE_PRIVATE);
        Editor prefEditor = preferences.edit();
        prefEditor.putString(key, value);       
        prefEditor.commit();
    }

To retrieve from shared pref

public synchronized String getFromPref(Context context, String key) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCE_NAME,
                Context.MODE_PRIVATE);
        return pref.getString(key, null);
    }
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243