0

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

user45678
  • 1,504
  • 6
  • 29
  • 58
  • 2
    "I do not want to use database for this operation" -- that's fine, but `SharedPreferences` would also not seem to be a good candidate. Consider storing your data in your own file structure (e.g., JSON). – CommonsWare Mar 30 '15 at 11:54
  • But i have seen many people using arraylist for SharedPreferences. Their review was positive to make it use for small purpose rather than having bigger database for it. – user45678 Mar 30 '15 at 11:55
  • 2
    "But i have seen many people using arraylist for SharedPreferences" -- `SharedPreferences` has no support for `ArrayList`. The closest thing is support for `Set`. You can hack something on top of `SharedPreferences` (e.g., unique keys with an array position mangled into the key), but I fail to see any value in doing that over storing the file yourself in a more compact and efficient format. – CommonsWare Mar 30 '15 at 11:57
  • If you don't want to use a database (**why?!**), then use a csv (or any textual file you might find fit) file. But don't complain for the limits you will get. – Phantômaxx Mar 30 '15 at 12:04

2 Answers2

0

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

Community
  • 1
  • 1
Bill Mote
  • 12,644
  • 7
  • 58
  • 82
0

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.

n.arrow001
  • 1,090
  • 3
  • 10
  • 28