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)

devpelux
  • 2,492
  • 3
  • 18
  • 38
  • Look at this question: http://stackoverflow.com/questions/4064901/how-to-use-sharedpreferences-as-localstore-in-more-generic-way – SaDeGH_F May 18 '14 at 18:43
  • Use GSON. Here is your solution: - http://stackoverflow.com/a/20848025/1787135 – jpardogo May 18 '14 at 20:44

3 Answers3

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;
}
matiash
  • 54,791
  • 16
  • 125
  • 154
  • 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! – devpelux May 19 '14 at 18:10
  • @Salvuccio96 In case I wasn't clear, this method does preserve duplicates, just call putString(). I was warning against putStringSet(). – matiash May 19 '14 at 18:34
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

Community
  • 1
  • 1
Kosh
  • 6,140
  • 3
  • 36
  • 67
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*"));
Ronn Wilder
  • 1,228
  • 9
  • 13
  • the split function here will see for spaces zero or more then a literal comma and then again spaces zero or more. – Ronn Wilder May 18 '14 at 20:15