0

I need to know the best way to add and get a Vector object to the SharedPreference in android.

I have vector with items for username and their corresponding status.The status is boolean.Suppose i have this vector with respective status for 10 users.I want to know how to add this vector to the SharedPreference of my application. i will do the get and use it for processing in different part.Kindly help me to do the get and set for this requirement.

JVN
  • 239
  • 2
  • 16

1 Answers1

2

First, Vector has been considered to be obsolete for ~15 years. Use ArrayList or CopyOnWriteArrayList, depending upon your threading requirements.

Second, SharedPreferences has no built-in means of storing a collection, other than a Set<String>.

You are welcome to:

  • Convert your data structure into a string (e.g., JSON) and store the string in SharedPreferences; or

  • Use a "numbered keys" approach, so a SharedPreferences key of stuff.0.username points to the 0th username, stuff.0.status points to the 0th status, stuff.1.username points to the 1st username, and so on; or

  • Use some other data persistence approach (e.g., SQLite database, JSON file) that may be more amenable to your data structure.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • ok thanks. i will try these ways... can you tell me why vector is not a good option? – JVN Aug 21 '15 at 09:11
  • @JVN: http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated – CommonsWare Aug 21 '15 at 11:15