无法使用JAVA在Android的SharedPreferences中存储ArrayList <string>?</string>

时间:2014-02-07 13:09:51

标签: java android arraylist sharedpreferences

这是在Android中的SharedPreferences中保存ArrayList但在此LOC处出错的代码:

 myAList.add(sEdit.getString("val"+j,""));

BY表示

  

对于SharedPreferences.Editor类型,方法getString(String)未定义方法getString(String)未定义类型SharedPreferences.Editor

public static void setValuesInSession(Context c,ArrayList<String> myArrayList)
{
    SharedPreferences sPrefs=PreferenceManager.getDefaultSharedPreferences(c);
    SharedPreferences.Editor sEdit=sPrefs.edit();

    for(int i=0;i<myArrayList.size();i++)
    {
             sEdit.putString("val"+i,myArrayList.get(i));
    }
     sEdit.putInt("size",myArrayList.size());
     sEdit.commit();

}
public static ArrayList<String> getValuesOfSession(Context c)
{
    SharedPreferences sPrefs=PreferenceManager.getDefaultSharedPreferences(c);
    SharedPreferences.Editor sEdit=sPrefs.edit();
    ArrayList<String> myAList=new ArrayList<String>();
    int size=sPrefs.getInt("size",0);

    for(int j=0;j<size;j++)
    {
     // Log.i("MYlIST ADD",""+);
         myAList.add(sEdit.getString("val"+j,""));
    }
    return myAList;
}

3 个答案:

答案 0 :(得分:1)

您正在尝试从编辑器获取字符串值,而不是从SharedPreferences中检索它。

替换

myAList.add(sEdit.getString("val"+j,""));

myAList.add(sPrefs.getString("val"+j,""));

所得

public static ArrayList<String> getValuesOfSession(Context c)
{
    SharedPreferences sPrefs=PreferenceManager.getDefaultSharedPreferences(c);
    ArrayList<String> myAList=new ArrayList<String>();
    int size=sPrefs.getInt("size",0);

    for(int j=0;j<size;j++)
    {
     // Log.i("MYlIST ADD",""+);
         myAList.add(sPrefs.getString("val"+j,""));
    }
    return myAList;
}

并不建议调用以“s”开头的局部变量,因为Android Code Style指南鼓励调用以s开头的非最终静态字段和以m开头的非公共成员字段。

答案 1 :(得分:0)

试试这个......

for(int i=0;i<myArrayList.size();i++)
    {
             sEdit.putString("val"+i,myArrayList.get(i).toString());
    }

下面是我的代码..我和你一样......工作......

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
             SharedPreferences.Editor editor = preferences.edit();
             editor.putInt("Size", languageNames.size());

             for(int i=0; i<languageNames.size(); i++)
             {
                 editor.putString("data" + i, languageNames.get(i).toString());
             }

             editor.putString("ID",id);
             editor.putString("Email", email);
             editor.putString("Birthday", birthday);
             editor.putString("Username", username);
             editor.putString("Location", location);
             editor.putString("Gender", gender);
             editor.commit();

获取数据......

ArrayList<String> languageNames = new ArrayList<String>();

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

            int array_size = preferences.getInt("Size", 0);
            id = preferences.getString("ID", "");
            username = preferences.getString("Username", "");
            email = preferences.getString("Email", "");
            birthday = preferences.getString("Birthday", "");
            location = preferences.getString("Location", "");
            gender = preferences.getString("Gender", "");  

            languageNames.clear();
            for(int i=0; i<array_size; i++)
            {
                languageNames.add(preferences.getString("data" + i, "").toString());
            }

答案 2 :(得分:0)

       public static ArrayList<String> getValuesOfSession(Context c) {
       SharedPreferences sPrefs=PreferenceManager.getDefaultSharedPreferences(c);
       ArrayList<String> myAList=new ArrayList<String>();
       int size=sPrefs.getInt("size",0);

       for(int j=0;j<size;j++)
       {
        // Log.i("MYlIST ADD",""+);
            myAList.add(sPrefs.getString("val"+j,""));
       }
       return myAList;
   }