user1771240
user1771240

Reputation:

can i store two or more values with same key using SharedPreferences in android?

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.

Upvotes: 7

Views: 5434

Answers (6)

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

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

Upvotes: 3

philipp
philipp

Reputation: 4183

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.

Upvotes: 3

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.

Upvotes: 0

Rasel
Rasel

Reputation: 15477

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.

Upvotes: 2

Ali Imran
Ali Imran

Reputation: 9217

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.

Upvotes: 0

Michał Kisiel
Michał Kisiel

Reputation: 1050

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

Upvotes: 1

Related Questions