2015-08-08 45 views
0

我保存一個字符串設置在加載到我的監視列表的共享偏好設置中。當用戶點擊一個按鈕時,在電影的頁面中,它將電影的名稱,標題和發佈日期存儲在一個JSON對象中。所有這些都存儲爲字符串。當我嘗試在觀察列表中添加一部電影時,問題仍然存在,每添加一部新電影,最後一部電影被覆蓋,這意味着我一直只能在觀看列表中看到一部電影。共享prefrence覆蓋

mButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      SharedPreferences sharedPreferences = getSharedPreferences("WatchlistData", Context.MODE_PRIVATE); 
      SharedPreferences.Editor editor = sharedPreferences.edit(); 

      JSONObject obj = new JSONObject(); 
      try { 
       obj.put("name", name); 
       obj.put("date", releaseDate); 
       obj.put("platform", platform); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      Set<String> set = new HashSet<String>(); 
      set.add(obj.toString()); 

      editor.putStringSet("setOfStrings", set); 



      editor.commit(); //saved 

     } 
    }); 

關注預訂片段OnCreateView

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_watchlist, container, false); 
    mRecyclerView = (RecyclerView) view.findViewById(R.id.watchlist); 
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
    mWatchlistAdapter = new WatchlistAdapter(getActivity(), loadFromStorage()); 
    mWatchlistAdapter.notifyDataSetChanged(); 
    mRecyclerView.setAdapter(mWatchlistAdapter); 
    return view; 
} 

的loadFromStorage,加載在recyclerview我的XML文件中,這個方法在我的適配器構造函數。

SharedPreferences mPrefs = getActivity().getSharedPreferences("WatchlistData", Context.MODE_PRIVATE); 

    ArrayList<watchlist> items = new ArrayList<watchlist>(); 

    Set<String> set = mPrefs.getStringSet("setOfStrings", null); //retrieving set of strings 

    if (set == null){ 
     Toast.makeText(getActivity(), "No data found", Toast.LENGTH_LONG).show(); 
    } else { 
     //for every string in set 
     for (String s : set) { 
      try { 
       JSONObject jsonObject = new JSONObject(s); //for every JSONObject String 
       String name = jsonObject.getString("name"); 
       String date = jsonObject.getString("date"); 
       String platform = jsonObject.getString("platform"); 

       watchlist newGame = new watchlist(); 

       newGame.setGame(name); 
       newGame.setDate(date); 
       newGame.setPlatform(platform); 
       items.add(newGame); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    return items; 
} 

回答

0

默認情況下SharedPreferences用較新的數據替換舊數據。你可以做的是,讓舊數據第一,然後用追加與新的一個,

//Extract the value stored in SharedPreferences 
String value = prefs.getString(<Key>, <DefaultValue>); 

//Append to the extracted value 
String appendedValue = append(value, newValue); 

//Write the result back to SharedPreferences 
editor.putString(<Key>, appendedValue).commit(); 

SharedPreferenced是爲精簡版重量數據重發存儲但不。因此,根據您的要求,您應該將數據寫入外部存儲中的JSON/XML文件並稍後再讀取。

+0

你能給我一個關於在外部存儲中保存爲JSON/XML的教程嗎? –

0

看起來你只是覆蓋每次你把東西有時間在SharedPreferences的stringset,這就是爲什麼你永遠只看到最後的結果:

 Set<String> set = new HashSet<String>(); 
     set.add(obj.toString()); 

     editor.putStringSet("setOfStrings", set); 

你只是創建了一個新的「設置'每次點擊並保存到「setOfStrings」。

我正在考慮一個類似的實現我自己的項目,但看到它成爲從SharedPreferences操縱StringSet的問題。作爲參考,[Android開發人員網站上的文檔](http://developer.android.com/reference/android/content/SharedPreferences.html#getStringSet(java.lang.String,java.util.Set)) states, 「請注意,您不得修改此調用返回的set實例。你做的,也不是你修改實例的能力。「

對此有一些解決方法,但根據我的理解,最好避免使用SharedPreferences來處理這種常常會被操作的數據,因爲它只是保存爲一個容易出現問題的XML文件 - 您可能需要考慮在SQLite數據庫中實現這些信息。

+0

您確定沒有其他解決方法? –

+0

您能否給我一個關於在外部存儲中保存爲JSON/XML的教程? –