I want to create some a dynamic list some thing like hash table
so I can put a key with value or I read list
I want to save it and able to read it when pogrom started .
is it possible ?
Seems like you are looking for HashMap
You will need to serialize / deserialize the map to save/load it.
you are asking for something like hash table where you can save key value pairs you can use also shared preferences for that.
http://as400samplecode.blogspot.in/2011/11/android-shared-preferences-example_12.html
Question: is it possible? Answer: yes, it is.
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.
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.
The thing is I need to store something like:
100 strings
20-30 ints
As you can see, it's a pretty large number, and you might say "Use sql"! I would use sql but for me it looks kinda alien. It's a lot of code, and after that I can't find a tutorial that shows you: look, you make a database with tables like this, and then you read it like this, you can also search in it like this...
Can you please give me the best method to save that data? And if you say sql, please give me a tutorial or something that shows me how to do what I said above.
I would say you should look into SQLite and try to learn that. But if you really want to stay away from SQL you could use putStringSet to store your strings in SharedPreferences. Now there isn't a thing for putIntSet but you could convert your ints to strings using String.valueOf(myInt) and save it using the same putStringSet. I would like to be clear though, I think that a database is the way to go on this but this method should work for you.
If you use SQl, look at ORMLite, which will let you store objects in the db (http://ormlite.com/). For 100 strings, if they're basically name/value pairs, just use SharedPreferences http://developer.android.com/reference/android/content/SharedPreferences.html. Could also use static string resources, as mentioned.
hey,
i have a dictionnary like
"key2","value2"
"key3","value3"
"key1","value1"
is it possible to sort it on value ?
If not, any other object can do same thing? (sort value on key/value pair)
Well, clearly you're talking about a Map.
It really depends on what Map you're using, You don't need to sort a TreeMap, coz it is already sorted.
For any other, You can get the keys using map.keySet() and use Collections.sort() to sort them. (Note, the map will remain the same, you'll get a Set containing all the key values)
Hope it helped.