0

I've found several similar pages that haven't quite been able to address my dilemna. Here goes:

I would like to be able to create a SharedPreferences list in Android that is extractable with an iterator (loop). Somehow I need to be able to add to a list with a key-value of "[Arrayname]+[element_number]" format. I also need to be able to add/remove values at will.

-- How will I be able to add to the end if I don't know how long the current SharedPrefs list is?

-- I also need the length of the SharedPrefs list to create arrays from it.

Cœur
  • 37,241
  • 25
  • 195
  • 267
AlleyOOP
  • 1,536
  • 4
  • 20
  • 43

2 Answers2

1

You get the a map of all values contained into a SharedPreferences instance with the getAll() method. You can then convert it to a list.

Thomas Bouron
  • 613
  • 3
  • 11
1

I suggest working with a regular in data container (e.g. ArrayList or HashMap).

you can store the container in the shared preference by serializing it: https://stackoverflow.com/a/5816861/1393632

Now you just need to keep it in sync with your shared preference when updating it.

Community
  • 1
  • 1
asaf gitai
  • 400
  • 2
  • 10