I just created a list view which consists of 2 list items as Colors and Temple. Colors and Temple again having a list items of Plans and Views. When I click on the Colors I should get the contents of Colors Plans and Colors Views. And when I click on the Temple I should get the contents of Temple Plans and Temple Views. How should I have to store the state and how can I get the contents using shared preferences.Please help me ..
SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared.
Follow the link of the official developer site for more:
https://developer.android.com/training/basics/data-storage/shared-preferences.html
You can write to Shared Preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();
and then read from Shared Preferences
SharedPreferences sharedPref =
getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);
And also dont forget to get a handle
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
Hope this helps for now, do you need more support?
Related
I want to create a game that counts the clicks made in 60 seconds and saves the record. I would like this record to be saved on the device even after the app is closed. Is there some kind of variable that allows me to do this?
Android's Shared Preferences seem to be the most relevant option for you.
Refer to the official documentation for an in depth look:
https://developer.android.com/training/data-storage/shared-preferences
These code samples should help you as well:
To save a value into the Shared Preferences:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.integer.saved_high_score_default_key); //set a default/backup option
int highScore = sharedPref.getInt(getString(R.string.saved_high_score_key), defaultValue);
Make sure to keep the key identical between placing values into the preferences and retrieving.
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.integer.saved_high_score_default_key);
int highScore = sharedPref.getInt(getString(R.string.saved_high_score_key), defaultValue);
SharedPreferences is probably the place to start with this. You can view Android documentation related to it here along with code examples: Save key-value data
There are some other options for saving data in an Android app on the left pane of that page, but SharedPreferences is probably the most applicable based on your described use case.
Im creating a alarm app that will have multiple alarm.. so i want to store the times in shared preference. if there is any solution to store integer list in shared preference
SharedPreferences preferences = getSharedPreferences("prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("key", yourIntValue);
editor.apply();
Use editor.apply() to save preferences asynchronously. apply() is preferable in the most cases.
Use editor.commit() to save preferences synchronously.
I saw this on stack Need to save a high score for an Android game
This is what it told me
//setting preferences
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putInt("key", score);
editor.commit();
//getting preferences
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
int score = prefs.getInt("key", 0); //0 is the default value
I was wondering, should "key" be the string where the highscore is located? and does it matter what i name my prefs key.
Thanks for your time.
The Android Developers documentation is an excellent place to start with any question like this. SharedPreferences can be found here. SharedPreferences is a simple Key-Value pair storage, so if you put an int into your shared preferences as with the key "high score", then in the future, if you want to get that int, you need to use prefs.getInt("high score"). It doesn't matter what you name your keys, but it is generally good practice to use private static final string variables to store keys you will use on a regular basis.
You can name the string whatever you like. Click here for documentation. You can name it anything from "foo" to "bar".
the Html parse is very slow, so I thought of storing a static text within the sharded preferences to access it faster. Is it possible to store this somehow and retrieve it, so that it can be set without usage of Html.fromHtml?
This way I would just parse the file one time. Once it is in cache it should be much faster, if no parsing is required.
Yes you can store Static string in shared preferences.
Here is code which may help you..
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(YOUR_KEY, YOUR_STATIC_STRING);
editor.commit();
I want to make a reminder application without using a database. How can I save reminders (like reminder 1, reminder 2 goes on up to 3) in list view items so that by clicking any list view item, I can open that reminder set by me earlier?
There is a number of storage options available in Android. May be simply storing in files will work for you.
Well just for three reminders I might go for SharedPreferences
Use Shared Preferences for that.
Use below code for save data into shared preferences
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString(MY_NAME, "Sai");
prefsEditor.putString(MY_WALLPAPER, "f664.PNG");
prefsEditor.commit();
Use below code for get data from shared preferences
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String prefName = myPrefs.getString(MY_NAME, "nothing");
String wallPaper = myPrefs.getString(MY_WALLPAPER, null);
Shared Preferences
Internal Sotrage
External Storage
SqLite Databases
Network Storage