0

I have found a great tutorial about storing arrays in SharedPreferences. Every time I add one item to an array, I store it in SharedPreferences, but when loading the array I get null for all values. Size of the array is increased by 1 every time, but the elements are stored as null. Why?

String tname[] = new String[loadArray(t_name, this).length + 1];
for (int i=0; i<tname.length; i++)
{
    Log.i("tname[" + i + "]", tname[i] + "");  //null
}
tname[tname.length-1] = et_name;  //this is a string which is not null
saveArray(tname, t_name, NewSchedule.this);

public boolean saveArray(String[] array, String arrayName, Context mContext) {   
        SharedPreferences prefs = mContext.getSharedPreferences("sp_name", 0);  
        SharedPreferences.Editor editor = prefs.edit();  
        editor.putInt(arrayName +"_size", array.length);  
        for(int i=0;i<array.length;i++)  
            editor.putString(arrayName + "_" + i, array[i]);  
        return editor.commit();  
    } 

    public String[] loadArray(String arrayName, Context mContext) {  
        SharedPreferences prefs = mContext.getSharedPreferences("sp_name", 0);  
        int size = prefs.getInt(arrayName + "_size", 0);  
        String array[] = new String[size];  
        for(int i=0;i<size;i++)  
            array[i] = prefs.getString(arrayName + "_" + i, null);  
        return array;  
    }  

Am I using the found code the right way?

**SOLUTION based ony 10s' answer:

String tname[] = new String[loadArray(t_name, this).length + 1];
String[] prevPrefs = loadArray(t_name, this);
for(int i=0; i<prevPrefs.length; i++) {
   tname[i] = prevPrefs[i];  //0th element will be null, as well as e.g. the 4th when there are 3 elements
}
for (int i=0; i<tname.length; i++)
{
    if (tname[i] == null) {  //so we put the new value in place of the null
        tname[i] = et_name;
    }
}

//Arraylists are much handier than arrays
if (arr_names.size() > 0) arr_names.clear(); //first clear all elements
for (int i=0; i<tname.length; i++) {
   if (tname[i] != null) arr_names.add(tname[i]);
}
for (int i=0; i<arr_names.size(); i++) {
    if (arr_names.get(i) == null) {
        arr_names.remove(i);
        }
}

String[] tname2 = new String[arr_names.size()]; //create new array
for (int i=0; i<tname2.length; i++)  //popluate new array
{
    tname2[i] = arr_names.get(i);
}

saveArray(tname2, t_name, NewSchedule.this);
4

2 回答 2

1

问题在这里:

String tname[] = new String[loadArray(t_name, this).length + 1];

此行不会创建一个填充有值的数组:loadArray(t_name, this).length + 1实际上返回一个数字。这一切实际上被翻译为:

String tname[]= new String[5];

它实际上创建了一个具有空值的新字符串数组。

prevPrefs[]解决方案:然后您应该在从您的方法加载的临时项目中进行迭代,loadArray()并将它们添加tname[]如下:

String[] prevPrefs = loadArray(t_name, this);
for(int i=0; i<prevPrefs.length; i++) {
   tname[i] = prevPrefs[i];
}

然后你会用前一个的值填充数组,你可以像你已经做的那样将新值添加到最后一个空点。

于 2012-10-04T20:22:25.753 回答
0

这一行是绝对错误的:

String tname[] = new String[loadArray(t_name, this).length + 1];

这一行说 -> 从首选项加载数组,然后立即将新内存分配给 tname

因此,您将始终在 tname 成员中获得 null 。

您应该在从首选项加载数组本身之前获取数组大小,创建数组(根据大小),然后才从首选项中复制数组字符串

<<<<< 编辑 >>>>>>

    String tname[] = loadArray(t_name, this); // only load the array, don't need "new String[]"

    for (int i=0; i<tname.length; i++)
    {
        Log.i("tname[" + i + "]", tname[i] + "");  //null
    }

    tname[tname.length-1] = et_name;  //this is a string which is not null
    saveArray(tname, t_name, MainActivity.this);

  public String[] loadArray(String arrayName, Context mContext)
  {
    SharedPreferences prefs = mContext.getSharedPreferences("sp_name", 0);
    int size = prefs.getInt(arrayName + "_size", 0);
    String array[] = new String[size + 1]; // +1 for storing the next value that should be saved
    for (int i = 0; i < size; i++)
        array[i] = prefs.getString(arrayName + "_" + i, null);
    return array;
  }
于 2012-10-04T20:09:17.207 回答