2014-02-07 30 views
-1

這裏是代碼在這個LOC保存在Android的SharedPreferences ArrayList中,但得到的錯誤:無法使用JAVA將ArrayList <String>存儲在Android的SharedPreferences中?

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

通過指示

方法的getString(字符串)是未定義的類型SharedPreferences.EditorThe方法的getString(字符串)是未定義的類型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; 
} 

回答

0

嘗試THI小號...

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()); 
      } 
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的代碼風格指南鼓勵調用以s開頭的非最終靜態字段和以m開頭的非公共成員字段。

+0

是的,你的回答是正確的,可以幫助我保存整個列表,無論列表提供給函數 – user3233280

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; 
    } 
相關問題