1

I have an array of Strings, I need to save this array with SharedPreferences, and then read and display them on a ListView.

For now, I use this algorithm:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

//To save the strings
public void saveStrings(String[] str){
    int a = 0;
    int lenght = str.length;
    while (a<lenght){
        sp.edit().putString(Integer.toString(a), Integer.toString(str[a])).apply();
        a=a+1;
    }
}

//To read the strings
public String[] getStrings(){
    String[] str = new String [8];
    int a = 0;
    int lenght = 8; //To read 8 strings
    while (a<lenght){
        str[a] = sp.getString(Integer.toString(a),"Null");
        a=a+1;
    }
return str;
}

Is there a way to save and read the entire array, rather than a string at a time?

For this project I'm using API level 19 (Android 4.4.2 KitKat)

2

3 Answers 3

2

Well, you could use putStringSet(), but (a) it's only from API level 11 onwards, and (b) as its name says, it's for sets (which means that you will lose the original ordering and any duplicates present in the array).

We solved this problem by "encoding" collections into a string and using putString() and decoding them afterwards on getString(), with this pair of methods (for ArrayList, but should be easily convertible into array versions):

public String encodeStringList(List<String> list, char separator)
{
    StringBuilder sb = new StringBuilder(list.size() * 50);

    for (String item : list)
    {
        if (sb.length() != 0)
            sb.append(separator);

        // Escape the separator character.
        sb.append(item.replace(Character.toString(separator), "\\" + separator));
    }

    return sb.toString();
}

public List<String> decodeStringList(String encoded, char separator)
{
    ArrayList<String> items = new ArrayList<String>();

    if (encoded != null && encoded.length() != 0)
    {
        // Use negative look-behind with backslash, because it's used for escaping the separator.
        // Expression is "(?<!\)s" with doubling because of escaping in regex, and again because of escaping in Java).
        String splitter = "(?<!\\\\)" + separator; //$NON-NLS-1$
        String[] parts = encoded.split(splitter);

        // While converting to list, take out the escape characters used to escape the now-removed separator.
        for (int i = 0; i < parts.length; i++)
            items.add(parts[i].replace("\\" + separator, Character.toString(separator)));
    }

    return items;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer, but I understand that I can't use this method, because I have a lot of duplicates, and I can't lose the duplicates!
@Salvuccio96 In case I wasn't clear, this method does preserve duplicates, just call putString(). I was warning against putStringSet().
0

Set<String> set =list.getStringSet("key", null); Set<String> set = new HashSet<String>(); set.addAll(list of the string list u have); editor.putStringSet("key", set); editor.commit();
Please refer to this thread for further details Save ArrayList to SharedPreferences

Comments

0
//while storing
str is string array
String fin="";
for(i=0;i<n;i++){
    fin=fin+str[i]+",";
}
fin=fin.substring(0,fin.length()-1);

//while retrieving
List<String> items = Arrays.asList(fin.split("\\s*,\\s*"));

1 Comment

the split function here will see for spaces zero or more then a literal comma and then again spaces zero or more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.