Not able to get String value from SharedPreferences - android

I save some String data to SharedPreferences but unfortunately i am unable to get the string value from sharedPreferences.
This is my code to save the data to SharedPreferences
SharedPreferences prefs = this.getSharedPreferences(Config.PREF_NAME, Context.MODE_PRIVATE);
userPhone = etPhone.getText().toString();
prefs.edit().putString("userPhone", userPhone).apply();
This saves my number perfectly but when i try to retrieve it in the next activity i get this string instead "userPhone"
This is how i retrieve the string value
String phoneNumber = prefs.getString(Config.PREF_NAME, "userPhone");
Log.i("number", phoneNumber);
My logs show phoneNumber as a string instead of the value from the user input that i saved to sharedPrefrences.

For storing values into SharedPreferences you are using Editor and method call:
prefs.edit().putString(String key, String value)
And you did it right:
prefs.edit().putString("userPhone", userPhone).apply();
For retrieving data, we are using the same key as we used for storing. In your case, it is "userPhone".
So, you should do it with:
prefs.getString("userPhone", "Some default value");
But, you mixed key with preferences name and you called
prefs.getString(Config.PREF_NAME, "userPhone");

Here is the difference.
You are actually retrieving the value from:
String phoneNumber = prefs.getString(Config.PREF_NAME, "userPhone");
But you need to do :
SharedPreferences sharedPreferences = getContext().getSharedPreferences(Config.PREF_NAME, Context.MODE_PRIVATE);
String phoneNumber = sharedPreferences.getString("userPhone", null);

It should look like this in your second Activity.
SharedPreferences prefs = this.getSharedPreferences(Config.PREF_NAME, Context.MODE_PRIVATE);
String phoneNumber = prefs.getString("userPhone", "defaultValueIfNoPhoneAvailable");
Log.i("number", phoneNumber);
The second parameter of getString is the default value in case it has no mapping for the key.

Related

How to string to shared preference without an editText?

can I save strings to my sharedpreference without using an editText. Using an edit text this is the format of putting string to the shared preference right?
editor.putString("username", etUsername.getText().toString());
I want to happen is to get the users ID from the db for it to be saved on the sharedpreference.
editor.putString("userID", *******);
what should I put there? Thanks. I don't know how to do it.
And this is my db for the users.
myDB
editor.putString("userID", parameter);
yes, in second parameter you can save your sting whatever your want.
You're doing it right.
String stringValue = "BobSmith";
editor.putString("stringKey", stringValue);
This can be done like this:
String valueToBeSaved = etUsername.getText().toString();
editor.putString("userID", valueToBeSaved);
editor.apply();
DatabaseHelper myDatabaseHelper = new DatabaseHelper(Activity.this);
myDatabaseHelper.openDataBase();
String userId = myDatabaseHelper.getYourData(); //this is the method to query user UserId
myDatabaseHelper.close();
//now save userId in shared preferences
String valueToBeSaved = etUsername.getText().toString();
editor.putString("userID", userId);
editor.apply();
}
I hope youe are searching for this. If any doubts let me know.

how to get the value of json object which i have saved in sharedpreference from one activity to another activity?

This is first activity through which i'm saving data in sharedpreferences...
JSONObject obj = new JSONObject(response);
public static String userid;
userid = obj.getString("userid");
String otp = obj.getString("opt");
SharedPreferences sp=getApplicationContext().getSharedPreferences("SharedPrefs", Context.MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString("userid", userid);
editor.putString("opt", otp.toString());
editor.commit();
and i want the value of userid in second activity...
SharedPreferences sp = getApplicationContext().getSharedPreferences("Sharedprefs",Context.MODE_PRIVATE);
String userid = sp.getString("userid","");
String otp = sp.getString("opt", "");
i want the value of "userid" and "otp" which have been stored in "SharedPrefs" sharedprefs, and i dont want to give any default value for this....
Thanks in Advance :)
If above is your code you should change Sharedprefs in the second Preferences to SharedPrefs.
When you store data in SharedPreferences, make sure that Preference name is spelled correctly. It is case sensitive.
Find the difference between "Sharedprefs" and "SharedPrefs".
Try this way.
SharedPreferences sp = getApplicationContext().getSharedPreferences("SharedPrefs",Context.MODE_PRIVATE);
String userid = sp.getString("userid","");
String otp = sp.getString("opt", "");

how to store multiple key value pairs in shared preferences

I have username and password field I want to store them using shared preferences.please guide me in storing and retrieving these data using sharedpreferences.
sharedPref=getApplicationContext().getSharedPreferences("sharedf",Context.MODE_PRIVATE);
String secretKey = sharedPref.getString("imei_num", null);
if(null==secretKey){
editor.putString("imei_num",imei_of_the_device);
editor.putString("pin",pinPrimary.getText().toString());
editor.commit();
}
Store in SharedPreferences :
SharedPreferences prefs = getSharedPreferences("sharedf",
Context.MODE_PRIVATE);
prefs.edit().putString("imei_num",imei_of_the_device)
.putString("pin",pinPrimary.getText().toString()).commit();
And retrieve like this :
String imei = prefs.getString("imei_num", "default value");
String pass = prefs.getString("pin", "default value");
You can use prefs.getString("key","default") retrieve value saved for given key if the key is not found it will return default value instead of null

Android two different SharedPreference types with the same ID

Currently I want to store two different data types as SharedPreference in my android app. Is it possible to store them with the same key-value?
e.g:
int id = 123;
myBoolean = false;
myString = "hello";
SharedPreferences.Editor edit = this.getSharedPreferences("MyPrefs", MODE_PRIVATE).edit();
edit.putString(String.valueOf(id), myString);
edit.putBoolean(String.valueOf(id), myBoolean);
because currently, when I try to get the string value I get a ClassCast exception here:
SharedPreferences settings = getSharedPreferences("MyPrefs", MODE_PRIVATE);
String myString = settings.getString(String.valueOf(123), "def");
I get this exception:
java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
It is not possible, Shared Preferences is a key value pair (the key is unique). What your code does is replace the previous saved value. So, when you try to get the value you receive a boolean.

How to get the value store in SharedPreferences

I am storing userId at the time of registration in SharedPreferencesand. Now I want to access the SharedPreferences stored UserId value. Till now I tried this code:
prefrence = PreferenceManager.getDefaultSharedPreferences(this);
edit3 = prefrence.edit();
edit3.putInt("user_id", userid);
Log.e("Commit", "SharedPreferences");
edit3.commit();
And on the next activity I am using this for access:
prefr = PreferenceManager.getDefaultSharedPreferences(this);
value = prefr.getInt("user_id", "");
How do I do this?
You PUT in an INT and GET a STRING... Maybe that's causing a type cast error.
I see you changed your code, to get an int... the WRONG WAY!
You are doing so:
value = prefr.getInt("user_id", "");
Instead you should do so:
value = prefr.getInt("user_id", 0);
(You can't assign "" to an int.)
The above is valid if user_id is an int. If user_id is a string, then you should do a putString and a getString. Like:
edit3.putString("user_id", userid);
And then
value = prefr.getString("user_id", "");
So you grant the necessary type consistency.
Try changing
prefr.getString("user_id", "");
to
prefr.getInt("user_id", 0);

Categories

Resources