SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = prefs.edit();
editor.putString("Orderinfo", responseStr);
Log.i("myprev",responseStr);//storing correct values
editor.commit();
I have used fragments,i stored Orderinfo,its stores the current values but in next fragment page i have to retrive my current customer data,but its shows previously stored customer details...can anyone help me to solve the problem?
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String emailsts = prefs.getString("Orderinfo", "");
Log.i("myinfo",emailsts);//this shows prev customer details
SharedPreferences myPrefs =PreferenceManager.getDefaultSharedPreferences(getActivity());
myPrefs.edit().putString("Orderinfo", responseStr).apply();
String responseString= myPrefs.getString("Orderinfo", "");
if(!responseString.isEmpty()){
// Do you code
}
String emailsts = prefs.getString("Orderinfo", "");
here "emailsts" is getting null so you will get default value which is blank in above code .which means your value is not Store in SharedPreferences.
And Mau be your "responseStr" is null please put log there and print value.
Log.v(TAG ,"responseStr=="+responseStr);
Related
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", "");
I am using following code:
SharedPreferences sharedPref = getSharedPreferences(GlobalDefines.SHARED_PREFERENCES, Context.MODE_PRIVATE);
String test = sharedPref.getString(GlobalDefines.GCM_KEY, "");
SharedPreferences.Editor editor = sharedPref.edit();
editor.clear();
editor.putBoolean(GlobalDefines.USER_IS_LOGGED_IN, false);
editor.remove(GlobalDefines.USER_NAME);
editor.remove(GlobalDefines.USER_PASSWORD);
editor.commit();
test = sharedPref.getString(GlobalDefines.GCM_KEY, "");
The string "test" has a value when I get the value from the shared preferences for the first time; when I remove another value from the preferences and want to get the same value (GCM_KEY) again, it is returned empty.
Why is that?
editor.clear() tells the editor that you want to remove ALL values from your SharedPreferences. Remove this line and you will see the expected behavior.
In android, i am adding string values using shared preferences, but i want to compare the value which i am going to add to shared preferences with values which are already stored in shared preferences to avoid adding duplicate values, but i am not getting how to do this?
or is there any alternate method to avoid adding duplicate values in shared preferences?
I am adding string values using following code
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putString(Name, s);
editor.commit();
In android you cannot really have duplicate value in sharedPreference because every time you change or modify a value on sharedPreference it will replace the previous with the current. So since every instance of it has a single unique key, which mean it will always be unique (in my experience every time i messed up with this keys like giving the same name key for both an Int and boolean for example i end up crashing the app or having some kind of exception)
If im wrong i hope someone else will correct me and provide you with a better answer!
I don't know whether I'm understanding your question quite well or not, but Android's SharedPreferenceshas it's own contains to check if a a key already exists or not.
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
if (sharedpreferences.contains(NAME)) //It already contains NAME key
On the other hand, if your worries are about a single key's value not to be repeated, just read it before storing the new value and compare themselves, no more.
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
if (!sharedpreferences.getString(NAME, "").equals(s)) {
// It does not have the same value, store 's'
sharedpreferences
.edit();
.putString(NAME, s);
.commit();
}
However, in this particular case I wouldn't perform this verification, just overwrite the value and that's it, as it always gonna be the same.
First get String value from SharedPreferences as oldvalue then compare with newvalue which you want to store. If String not match then save newvalue in SharedPreferences.
Try something like this
String str_newvalue = "new string here";
SharedPreferences sharedpref = this.getSharedPreferences(this.getPackageName(), context.MODE_PRIVATE);
String str_oldvalue = sharedpref.getString("key", "");
if (!str_newvalue.equals(str_oldvalue)) {
sharedpref.edit().putString("key", str_newvalue).commit();
}
Do this
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if(restoredText.matches(your string))
{
// do nothing
}
else
{
//save your data
}
}
Im having trouble with retrieving username and password from android's sharedpreferences. I use this code to save the username and pass
SharedPreferences prefs=getSharedPreferences("File", 0);
SharedPreferences.Editor e= prefs.edit();
e.putString("Email", "example#example.com").putString("Password", "password1");
e.commit();
e.putString("Email", "example_2#example.com").putString("Password", "password2");
e.commit();
String s=prefs.getString("Email","not found");
But i dont know how to retrieve information for user to log in. Can anybody help me figure out
Create Share Preference:
SharedPreferences sp=getSharedPreferences("Login", 0);
SharedPreferences.Editor Ed=sp.edit();
Ed.putString("Unm",Value );
Ed.putString("Psw",Value);
Ed.commit();
Get Value from Share preference:
SharedPreferences sp1=this.getSharedPreferences("Login",null);
String unm=sp1.getString("Unm", null);
String pass = sp1.getString("Psw", null);
You need to give different keys for different values, otherwise the second email will erase the first one. See shared preferences as a persistent hashmap :
//keep constants, don't use their values. A constant has more meaning
SharedPreferences prefs=getSharedPreferences("File", MODE_PRIVATE );
SharedPreferences.Editor e= prefs.edit();
//keys should be constants as well, or derived from a constant prefix in a loop.
e.putString("Email1", "example#example.com").putString("Password1", "password1");
e.putString("Email2", "example_2#example.com").putString("Password2", "password2");
//commit once, not twice
e.commit();
//not found should be a constant in a xml resource file
String mail1=prefs.getString("Email1","not found");
String mail2=prefs.getString("Email2","not found");
I am new to android and java too. Can anyone please suggest how to use shared preferences to stack history of products searched and push and remove when it reaches a certain number.
I have product list in a listview in activity1 and product details in activity2.
In activity2 context menu I want to add Add to fav and History of searched
products. Once a product is added i want that fav context menu to disable. How can I stack
history..It is a offline app.....push and remove once the limit of history reaches.
How can I do that ..? Thank you..
//Obtain shared preferences
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
//obtain boolean value stored in preferences
boolean booelanExample = settings.getBoolean("boolean_example", false);
//obtain string value stored in preferences
String stringExample = settings.getString("string_example", "");
//Obtain settings editor put new values and commint again
Editor settingsEditor = PreferenceManager.getDefaultSharedPreferences(this).edit();
settingsEditor.putString("string_example", "stringvalue");
settingsEditor.putBoolean("boolean_example", false);
settingsEditor.commit();
To obtain shared preferences, use the following method
In your activity:
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
To read preferences:
String dateTimeKey = "com.example.app.datetime";
// use a default value using new Date()
long long = prefs.getLong(dateTimeKey, new Date().getTime());
To edit and save preferences
Date dt = getSomeDate();
prefs.edit().putLong(dateTimeKey, dt.getTime()).commit();