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();
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);
I usually use this code for creating sharedPrefs file:
SharedPrefrences SP = getSharedPrefrences("MYPREF",0);
SP.getString("Username","x");
But now if I see this directory:
"/data/data/packageName/"
There is no shared_prefs directory.
Another thing : if I use Editor its work properly.
First you need to store some data in shared preferences using apply()/commit() method so that you retrieve data from there.
SharedPrefrences SP = getSharedPrefrences("MYPREF",0);
SP.edit().putString("Username","SOME NAME").apply();
System.out.println("Username is : "+SP.getString("Username","x"));
First of all you have to SET the data with an Editor
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Name","Pepito");
editor.apply();
Then you can read it as follows :
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String name = preferences.getString("Name", "Here's the default value");
If you've set the value after make that getString() your String value will be "Pepito", otherwise you'll get "Here's the default value"
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 :)
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;
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/