I am doing a simple application where a ListView is populated from the data stored in SharedPreferences. I need to read all the key pair values from SharedPreference. I used the code given in another post with same question, but it has not helped me at all.
The code is using a Map to getALL() from SharedPreference. When I try to print the count() of keys in the MAP it is always giving zero count. I am stuck in my application building due to this problem.
Can somebody help me with a simple code to retrieve all the key and values from a SharedPreference? Thanks.
You can store and retrive arraylist or array data like this Example.
You are fetching data into list but you are not storing data into sharedprefence from arraylist.
In this situation(never tried but this may work)
Store
retrive size variable as in above example
increament size by 1(to store one value)
store value in SharedPreference
Store size in SharedPreference.
Fetch
retrive size variable as in above example
then go through loop for all values
Related
I was wondering if there is a way to put data in Sharedpreferences on Android without creating a String. I have a sensitive char array(base64 encoding) that I would like to store in an encrypted Sharepreference but I would like to avoid creating a String(using putString()) in the process given that they are immutable. Is there a way to accomplish this?
Thanks!
Well for this scenario I can suggest you can hide actual content with key-value pattern, just create any significant key for your data at point of entry and set data as value for same and save both in SQL-lite or any supported database, and then retrieve same values using key at your destination.
By this way only key will be shared in internal transaction and data will never reveal on any mid-points.
Hope this will help.
I am trying to fetch a data from an Sqlite and bind it to a Recyclerview.The problem I am facing here is it is taking so long to get all the data from the database and add it to an Array List(I am using AsyncTask). I also have tried to fetch only the datas which matches to the text I am looking for,Still it takes long time. I tried to save the ArrayList in a SharedPreferences and use it when needed but I couldn't save ArrayList of a specific class to a shared Preference(it doesnt support). which way can i solve this problem?
I apologize for my English.
I'm using a RecyclerView as a checklist that can be used as a grocery list for example, first the user adds the product(item), the price and the store, then my app adds this item object into my ArrayList adapter; each item in my adapter is a relative layout with 3 TextViews for the product, price and store. The problem occurs when the user shuts down his device or closes the app: the RecyclerView resets to nothing.
What is the easiest way to save items in a RecyclerView so that it stays? Do I save in the cache?
Shared Preferences
or
SQLite Databases
You will need to save the data to persistent storage in some way. For something small, it would be best to use SharedPreferences. Then, when you re-open the app, load the data from SharedPreferences into your ArrayList, then populate the RecyclerView.
To use SharedPreferences you will need to convert your data into a String, because SharedPreferences can only handle primitive data types. The best way to convert lots of data to a String is by converting the data into a JSONObject, then that JSONObject into a String (which can be stored in SharedPreferences).
JSON is like a dictionary, it's a way to store Key-Value pairs. See this SO question for more information and some examples of what it looks like. Then search for how to use JSON and Android (or reference the docs).
EDIT: If your RecyclerView is showing a simple amount of data, you may be able to store a Set<String> in your SharedPreferences. See this relevant documentation.
When i write anything into shared preferences it overwrites the previous value, how can i continuously inject values in preferences and fetch it out all at once like an array? I do not want to use database for this operation.
Thanks
You can use JSON as #CommonsWare points out to store your data in SharedPreferences. I have such an answer that using GSON here:
https://stackoverflow.com/a/5968562/617044
Though not ideal, if you want to use Preferences only, You can store String in preferences. When next value is to be added, retrive the old one add 'comma' and new value.
Whenever you want to use these values, use StringTokeniser. Tokenise elements by 'comma' character.
can i store two or more values with same key using SharedPreferences in android? If no, please tell me how to store values of username, first name, password etc when many users register in registration app?
Ex:
person A registered with username="john12", first name="john" and DOB="06/06/2000".
person B registered with username="arun89", first name="arun" and DOB="08/11/1989".
Now, I want to store these values in SharedPreferences and retrieve them later. Is it possible using SharedPreferences? If not, Please tell me how to do in other way.
Thank you in advance.
I woud consider creating a JSONObject and add the fields you want to store as a key:value pair.
json.putString(key, value);
You can then store the json object in it's string representation with json.toString() and restore it later with
JSONObject jo = new JSONObject(jsonString);
String value = jo.getString(key);
JSONObject also offeres different data types beside strings.
It really depends on how much data you want to store. Depending on that I would choose SharedPreferences or a SQLite implementation.
You cannot store these values directly (as ones added latter will overwrite previously added) but you can always store Parcelable and put your data into it
For your case it is better use SQLIte database.But if you want to use shared preference it is still possible.You have to use a key with additional index to remember different user like
UserName1:arun
UserName2:john
You have to remember the total number of user.Then can maintain all of them.you can also use other data structure like hashmap to maintain data for the shared preference.
I dont't think it is possible, as you don't know the number of users.
You could try to separate the users with commas, but that's lame.
You should consider using SQLite database.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
Then you have to store a array List of user objects first create a class userInfo then create a array List type of userInfo then store the data in this list and put a serialize-able object in SharedPreferences.
You can also store them on a single key called "registers" as string. Concatenate each register to preference. Put ";" (or any other characther you want) between each register. Then parse the string and use the values.
Key: registers
Value: "username=john12, first name=john, DOB=06/06/2000;username=mike12, first name=mike, DOB=06/07/2012"
Using split method of String will give you a list of registers as String.
registers.split(";");
Splitting again with "," will give you properties of each register.