如何保存字符串与共享preferences一个数组? [英] How to save an array of strings with SharedPreferences?

查看:221
本文介绍了如何保存字符串与共享preferences一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串数组,我需要保存这个数组共享preferences,然后读取和一个ListView显示出来。

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

现在,我用这个算法:

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?

对于这个项目,我使用API​​级19(是Android 4.4.2奇巧)

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

推荐答案

好了,你可以使用<一个href=\"http://developer.android.com/reference/android/content/Shared$p$pferences.Editor.html#putStringSet%28java.lang.String,%20java.util.Set%3Cjava.lang.String%3E%29\"相对=nofollow> putStringSet(),但(一)这只是从API级别11起,和(b)如它的名字一样,它对于设置(这意味着你将失去原来的排序和阵列中的任何重复present)。

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).

我们的编码集合为一个字符串,并使用 putString(),之后就进行解码,从而解决了这个问题的getString(),用这双方法(ArrayList的,但应该是很容易兑换成阵列版本):

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;
}

这篇关于如何保存字符串与共享preferences一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆