1

I have a probleme here.

I don't know how to read all values of a SharedPreferences for one particular key. Actually I'm trying to write an Arraylist in preferences , then read it.

Let me explain with some code, Here is my methods for write in preferences :

fun writeArrayOnPreferences(key: String?, array: ArrayList<String>, c:Context) {
        val preferences = c.getSharedPreferences(
                c.getString(key), Context.MODE_PRIVATE)
        with(preferences.edit()) {
            for (value in array) {
                putString(key, value)
            }
            commit()
        }
    }

My writing code works , it's persistent , but I don't really understand how to READ this Arraylist from preferences.

I tried a lot of things to read this but it show me only the last element wrote in preferences

I really want you to understand that I want multiple values for a specific key

Manu13k
  • 326
  • 1
  • 6
  • 20
  • I said hello guys but stackOverflow deleted it , so Hello :) – Manu13k Jun 27 '18 at 17:25
  • Heyho :) `SharedPreferences` doesn't keep multiple values for a key. `putString(key, value)` with the same key will replace the previous value. That's why it only shows the last key. To use the same key, all `String` values have to be added up to one `String` and that can be saved with the key. The answers show ways to do that. – aruh Jun 27 '18 at 17:57

4 Answers4

3

ok here how it is! If you want multiple data against one key in share pref editor then SET is your solution as After API 11 the SharedPreferences Editor accept Sets. You could convert your List into a HashSet or something similar and store it like that When your read it back, convert it into an ArrayList, sort it if needed and you're good to go.

        //Set the values
        val yourSet = HashSet<String>()
        set.addAll(listOfExistingScores)
        yourPrefEditor.putStringSet("key", yourSet)
        yourPrefEditor.commit()

        //Retrieve the values
        val yourSet = yourPref.getStringSet("key", null)

SOLUTION NUMBER 2

would be like serializing the ArrayList and passing it! but there can be a catch if any value in your array does posses any rule that can't be Parced it will crash!

For More check this Thread this is in java but it will help tell you more!

Rizwan Atta
  • 3,222
  • 2
  • 19
  • 31
3

This is a quick example based on Nabin Bhandari's answer

fun writeArrayOnPreferences(key: String, array: ArrayList<String>, c:Context) {
    val jsonString = Gson().toJson(array)
    pref.edit().putString(key, jsonString).commit()
}

fun readArrayFromPreferences(key: String, c: Context): ArrayList<String> {
    val jsonString = pref.getString(key)
    val array = Gson().fromJson(jsonString, ArrayList<String>()::class.java)
    return array
}
Yaswant Narayan
  • 1,297
  • 1
  • 15
  • 20
  • Thanks for this very nice example ! The "ArrayList()::class.java)" part helped me so much because I was lost – Manu13k Jun 27 '18 at 18:23
1

You cannot simply loop values in an ArrayList put them in the preferences using same key for all values and expect to retrieve the ArrayList back.

You can convert the 'ArrayList' in JSON format and store it in SharedPreferences. Then to parse the JSON string to get the ArrayList.

You can make this process easier with the help of a library called Gson.

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
0

Try this:

    with(preferences.edit()) {
        var s = "" 
        for (value in array) {
            s = s + value + ","
        }
        putString(key, value)
        commit()
    }

your array will be saved like comma separated values which when read back in a string, by using the split function will become an array.