I want to save in store a position in my shared preferences, and can edit it, and open it when app starts, thanks, i have this:
//load shared preferecnes
SharedPreferences sharedpreferences = getSharedPreferences("MyPrefs",
Context.MODE_PRIVATE);
//note: here idk how to read the last value of position saved
//Log.v("lastposition", sharedPreferences );
// on edit preferences and save
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("position", New LatLang(30,40) );
editor.commit();
Thanks everyone :)
LAST EDIT CHANGES:
//load shared preferecnes
SharedPreferences sharedpreferences = getSharedPreferences(this,Context.MODE_PRIVATE);
// --> HERE IDK HOT LOAD LAST POSITIONS SAVED
// on edit preferences and save
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putDouble("position_lat", 30d );
editor.putDouble("position_lon", 40d);
editor.commit();
sharedPreferences.getString("position", new LatLng(30,40).toString());
if your application has no information saved at "position", it needs to load a default value instead. Here it is LatLng(30,40).
But you can not save complex objects such as LatLng. What you can do instead, is saving/loading latitude and longitude values:
//load shared preferecnes
SharedPreferences sharedpreferences = getSharedPreferences(this,Context.MODE_PRIVATE);
//note: here you load the saved latitude and longitude values into the variable with name "loaded_position":
LatLng loaded_position = new LatLng(0,0);
loaded_position.latitude= sharedpreferences.getFloat("position_lat", 15f);
loaded_position.longitude = sharedpreferences.getFloat("position_lon", 15f);
Log.v("lastposition", "loaded position: ("+loaded_position.latitude+","+loaded_position.longitude+")" );
// on edit preferences, save the LatLng Object:
SharedPreferences.Editor editor = sharedpreferences.edit();
LatLng currentPosition = new LatLng(30f,40f);
editor.putFloat("position_lat", (float) currentPosition.latitude );
editor.putFloat("position_lon", (float) currentPosition.longitude);
editor.commit();
This code, loads your by editor saved (30,40) into loaded_position. It should load 15,15 instead on your first start of the application, because 30,40 is not saved at that first start in your sharedPreferences.
You can look at this page please:
http://developer.android.com/training/basics/data-storage/shared-preferences.html
Related
Guys I have developed a small game and I want to save high score on same screen.
I thought it's easy to save the integer but the problem is that I have no idea where to start can anyone guide me if you want I can give you my code
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
to save data
Editor editor = sharedpreferences.edit();
editor.putInt("key", your score goes here);
editor.commit();
and than to retrive your score back
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
int highScore = sharedpreferences.getInt("key" , defaultValue);
There you go
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 :)
I want to save a time from a TimePickerDialog to sharedpreferences from my settings menu. I then want to retrieve this data when from another fragment. The time is stored as a long.
In the setting menu - when the positive button is pressed
SharedPreferences preferences = context.getSharedPreferences("TIME", Context.MODE_PRIVATE);
SharedPreferences pref = context.getSharedPreferences(
"any_prefname", Context.MODE_PRIVATE);
Editor editor = pref.edit();
editor.putLong("key_name", 8);
editor.commit();
In the fragment:
SharedPreferences pref = getActivity().getSharedPreferences(
"any_prefname", Context.MODE_PRIVATE);
Long longValue = pref.getLong("key_name", 0);
Toast.makeText(getActivity(), "Hi " + longValue, Toast.LENGTH_SHORT).show();
The problem is that the value "8" that I saved is note being shown in the toast from the fragment. The value being used is the 0.
Thank you
You aren't using the same key. When saving you used "time", when loading you used "key_name". You need to use 1 name.
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();