2014-02-15 384 views
5

我保存arraylist大小和數據在sharedPreference.When我從sharedpreference檢索arraylist大小它給予什麼尺寸節省previous.But確切的大小,但我在保存Arraylist數據失敗。當我檢索arraylist數據它總是給列表列表的最後一項。如何在SharedPreferences中保存arrayList數據?

這裏是我救了ArrayList的大小和數據代碼:

ArrayList<Items> mArrayList1 = new ArrayList<Items>(); 
if(mArrayList1 == 0){ 
    mStoreItem.setItem(id); 
    mArrayList1.add(mStoreItem); 
    int size = mArrayList1.size(); 
    System.out.println("array list size : " + size); 
    MyPreferences.savePreferences(getActivity(), 
        "arraylist_size", Integer.toString(size)); 
    for (int i = 0; i < mArrayList1.size(); i++) { 
     String id = mArrayList1.get(i) 
       .getItem(); 
     MyPreferences.savePreferences(getActivity(), 
         "id", id); 
      } 
} else if(mArrayList1 > 0){ 
    mStoreItem.setItem(id); 
    mArrayList1.add(mStoreItem); 
    int size = mArrayList1.size(); 
    System.out.println("arrayList size : " + size); 
    MyPreferences.savePreferences(getActivity(), 
        "arraylist_size", Integer.toString(size)); 
    for (int i = 0; i < mArrayList1.size(); i++) { 
     String id = mArrayList1.get(i) 
       .getItem(); 
     MyPreferences.savePreferences(getActivity(), 
         "id", id); 
      } 
} 

我在這裏取回我的ArrayList項目:

String getListSize = MyPreferences.savePreferences(getActivity(), 
        "arraylist_size"); 
System.out.println("arrayList size : " + getListSize); 
    if (!getListSize.equals("")) { 
     int listSize = Integer.parseInt(getListSize); 

     if (listSize > 0) { 
      for (int i = 0; i < listSize; i++) { 
       String getListitems = MyPreferences 
         .getPreferences(getActivity(), "id"); 
    System.out.Println("list items : "+ getListItems); 

      } 
     } 
    } 

這是我的代碼我怎麼能存儲什麼錯在我的代碼在sharedpreference任何一個arraylistitems請指導我...

+0

請參閱此[save-arraylist-to-sharedpreferences](http://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences) –

+0

請參閱http://stackoverflow.com/questions/12150597/ save-an-arraylist-of-strings-to-shared-preferences – rajshree

回答

4

試試這個。

//Set the values 
Set<String> set = new HashSet<String>(); 
set.addAll(mArrayList1); 
scoreEditor.putStringSet("key", set); 
scoreEditor.commit(); 

//Retrieve the values 
Set<String> set = new HashSet<String>(); 
set = myScores.getStringSet("key", null); 

這個問題已經回答了下面的線程,Save ArrayList to SharedPreferences

public void addTask(Task t) { 
     if (null == currentTasks) { 
      currentTasks = new ArrayList<task>(); 
     } 
     currentTasks.add(t); 

     //save the task list to preference 
     SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE); 
     Editor editor = prefs.edit(); 
     try { 
      editor.putString(TASKS, ObjectSerializer.serialize(currentTasks)); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     editor.commit(); 
    } 

public void onCreate() { 
     super.onCreate(); 
     if (null == currentTasks) { 
      currentTasks = new ArrayList<task>(); 
     } 

     //  load tasks from preference 
     SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE); 

     try { 
      currentTasks = (ArrayList<task>) ObjectSerializer.deserialize(prefs.getString(TASKS, ObjectSerializer.serialize(new ArrayList<task>()))); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } 
    } 

這裏ü改變taskclass作爲itemClass時。

+0

感謝您提供reply.when我添加此代碼set.addAll(mArrayList1);在這裏我得到的類型設置不適用的參數(ArrayList )此錯誤的方法addAll(集合<?extends String>)。我的arrylist擴展Items類..如何可以將我的classtype arraylist添加到stringtypy arraylist 。 –

+0

用戶集 set = new HastSet () – RVG

+0

此putStringset不支持bellow Api級別11.如何將我的ArrayList數據保存到api 11以下的sharedpreference中。 –

相關問題