Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法存储ArrayList<;字符串>;在Android中使用JAVA的SharedReferences中?_Java_Android_Arraylist_Sharedpreferences - Fatal编程技术网

无法存储ArrayList<;字符串>;在Android中使用JAVA的SharedReferences中?

无法存储ArrayList<;字符串>;在Android中使用JAVA的SharedReferences中?,java,android,arraylist,sharedpreferences,Java,Android,Arraylist,Sharedpreferences,以下是在Android中将ArrayList保存在SharedReferences中的代码,但在此位置出现错误: myAList.add(sEdit.getString("val"+j,"")); 表明 类型SharedReferences.Editor的方法getString(String)未定义,类型SharedReferences.Editor的方法getString(String)未定义 publicstaticvoidsetvaluesinsession(上下文c,arraylis

以下是在Android中将ArrayList保存在SharedReferences中的代码,但在此位置出现错误:

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

类型SharedReferences.Editor的方法getString(String)未定义,类型SharedReferences.Editor的方法getString(String)未定义

publicstaticvoidsetvaluesinsession(上下文c,arraylistmyarraylist)
{
SharedReferences sPrefs=首选项管理器。GetDefaultSharedReferences(c);
SharedReferences.Editor sEdit=sPrefs.edit();
对于(inti=0;i试试这个

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

for(int i=0;i您正在尝试从编辑器中获取字符串值,而不是从SharedReferences中检索它

替换

myAList.add(sEdit.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;
}
publicstaticarraylistgetvaluesofsession(上下文c)
{
SharedReferences sPrefs=首选项管理器。GetDefaultSharedReferences(c);
ArrayList MyList=新的ArrayList();
int size=sPrefs.getInt(“size”,0);
对于(int j=0;j
publicstaticarraylistgetvaluesofsession)(上下文c){
SharedReferences sPrefs=首选项管理器。GetDefaultSharedReferences(c);
ArrayList MyList=新的ArrayList();
int size=sPrefs.getInt(“size”,0);

对于(int j=0;jyes)您的答案是正确的,您能帮我保存整个列表吗?无论我为函数提供了什么列表
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());
            }
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;
}
       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;
   }