Array of strings in SharedPreferences

前端 未结 4 1314
感动是毒
感动是毒 2020-12-06 13:27

I wanna know whether is it possible to store array of string in the SharedPreferences. In my application i want , set of names to be stored

相关标签:
4条回答
  • 2020-12-06 13:27

    You can store set of String using SharedPreferences in API Level 11 and higher. See getStringSet() and putStringSet()ю

    In API Level prior to 11 you can use some kind of hack. For example, if you need to store string array under key "stringArray", you can save each string from array using putString and keys "stringArray.1", "stringArray.2", so on.

    0 讨论(0)
  • 2020-12-06 13:27

    If you are looking for StringTokenizer then blog post 1 and blog post2 would be helpful

    0 讨论(0)
  • 2020-12-06 13:49

    In shared preferences, you can store the data like key value pairs. What I usually do is to insert all the data then get the key list and iterate through it, set key as anything unique, be it numbers 1,2,3 etc

    use

    SharedPreference sp = context.getSharedPreferences("myPrefs", MODE_WORLD_READABLE); Map presetDataMap = sp.getAll();

    then loop through presetDataMap,

    Iterator itt = presetDataMap.keySet().iterator();

    hope this helps.

    0 讨论(0)
  • 2020-12-06 13:52

    It's not possible to store them as an array, but you can concatenate them, and then split them when loading, using StringTokenizer. I can help with some code, if this will be helpful for you.

    0 讨论(0)
提交回复
热议问题