I am developing an application in which I want to implement the next and previous functionality. User can go forward or backward on stories through this functionality. I have stored current name of story in shared preferences but I am stuck now. I don't have idea of how to read the name of story defined in string.xml. So what should I do to implement next/previous functionality.
SharedPreferences currentstory = getSharedPreferences("myprefs",Context.MODE_PRIVATE);
String currstory= currentstory.getString("currentstory","mystory");
In sharedpreference I have stored the name of current story. So I will get the name of current story by getsharedpreferences() in anther activity. But I want to get the storyname of the previous story defined in the story.xml.
You can read string from string.xml, useing this code:
Resources resources = getResources();
final String storyName = resources.getString(R.string.story_name);
SharedPreferences currentstory = getSharedPreferences("myprefs",Context.MODE_PRIVATE);
String currstory= currentstory.getString("currentstory", storyName );
And save:
SharedPreferences currentstory = getSharedPreferences("myprefs",Context.MODE_PRIVATE);
Editor editor = currentstory.editor();
editor.putString("currentstory", "New Story Name");
editor.commit();
editor = null;
Related
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);
My application is basically a quiz that presents people with world flags. I want to add a save function that adds the current flag to a separate list. Right now, this is what I do when they click the "save" button:
saveListGC.add(playList.get(1));
(playList.get(1) is the current flag). The problem is, I have to re-define the saveListGC every time the script starts, and that empties the contents:
public static ArrayList<String> saveListGC = new ArrayList<String>();
So what I'm wondering is how can I save this data, and re-load it later? I've seen things about using SharedPrefernces, but I don't really understand it. If someone could please explain it as easily as possible, I would really appreciate it. Thank you!
This a simple example of how you can use SharedPreferences:
//intiat your shared pref
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
//store data to pref
editor.putString("myString", "value 1");
editor.commit();
//retrieve data from pref
String myString = pref.getString("myString", null);
But the real problem here is that SharedPreferences can not store an Object of type List (ArrayList), but you can store an Object of type Set (like Hashset) using this method
Set<String> mySet = new HashSet<String>();
mySet.add("value1");
mySet.add("value2");
mySet.add("value3");
editor.putStringSet("MySet", mySet);
So to answer your second question, this what i propose for you to do:
//This is your List
ArrayList<String> saveListGC = new ArrayList<String>();
//Convert your List to a Set
Set<String> saveSetGC = new HashSet<String>(saveListGC);
//Now Store Data to the SharedPreferences
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
editor.putStringSet("MySet", saveSetGC);
editor.commit();
//After that in another place or in another Activity , Or every where in your app you can Retreive your List back
pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Set<String> mySetBack = pref.getStringSet("MySet", null);
//Convert Your Set to List again
ArrayList<String> myListBack = new ArrayList<String>(mySetBack);
//Here you can se the List as you like...............
Log.i("MyTag", myListBack.get(0));
Log.i("MyTag", myListBack.get(1));
Good Luck :)
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
}
}
I have problem understanding shared preferences. I have activity where user will insert password, start price, waiting price etc. My plan was to set starting value, and than user would change that value if he wants.
My question is: If I create prefs in onCreate() method, how change would apply (using SharedPreferences.Editor) when every time i run application it should create new values in prefs.
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 l = prefs.getLong(dateTimeKey, new Date().getTime());
To edit and save preferences
Date dt = getSomeDate();
prefs.edit().putLong(dateTimeKey, dt.getTime()).commit();
No it will change the previous one by key....
http://mobile.tutsplus.com/tutorials/android/android-application-preferences/
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();