On read shared preferences , only one object received

56 Views Asked by At

I'm trying to save some Array of objects to SharedPreferences.

How it's working on my app:

I have RecyclerView that contains clickable items. On click on these items, they are added to a new Array. And the new array is shown in another view. I'm trying to add the array to the SharedPreferences on onDestroyView() of fragment. This means that when I kill the fragment, the array will be saved in SharedPreferences.

For now with my code, on read data I'm getting only the last object from the array, instead of all objects.

Here is my code:

private LightsHistory lightsHistory;
    private ArrayList<LightsHistory> lightHistories = new ArrayList<>();

Add item to another view:

public void addItemToHistory(int position) {
        String title = mLightsArray.get(position).getLampTitle();
        String desc = mLightsArray.get(position).getLampDesc();
        String imageURL = mLightsArray.get(position).getLampImageUrl();

        lightsHistory = new LightsHistory(imageURL, title, desc);
        lightHistories.clear();
        lightHistories.add(lightsHistory);

        for (int j = 0; j < lightHistories.size(); j++) {
            if (i == 3) {
                i = 0;
            }
            Glide.with(getContext())
                    .load(lightHistories.get(j).getmLightsIMG())
                    .override(150)
                    .into(historyItemsPH.get(i));
            i++;
        }


        saveToSharedPrefs(lightHistories);
    }

Save to SP

public void saveToSharedPrefs(ArrayList<LightsHistory> lightHistories) {
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
        SharedPreferences.Editor editor = sharedPrefs.edit();
        Gson gson = new Gson();

        String json = gson.toJson(lightHistories);

        editor.putString(TAG, json);
        editor.commit();
        Log.d(TAG, "saveToSharedPrefs: "+json);
    }

Read from SP

public void readFromSharedPrefs() {
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
        Gson gson = new Gson();
        String json = sharedPrefs.getString(TAG, "");
        Type type = new TypeToken<List<LightsHistory>>() {
        }.getType();
        ArrayList<LightsHistory> arrayList = gson.fromJson(json, type);
        if (arrayList != null) {
            for (int j = 0; j < lightHistories.size(); j++) {
                Log.d(TAG, "readFromSharedPrefs: " + arrayList.get(j).getmLightsTitle()
                        + "\n" + arrayList.get(j).getmLightsDesc()
                        + "\n" + arrayList.get(j).getmLightsIMG());
            }
        }
    }
2

There are 2 best solutions below

0
On
lightsHistory = new LightsHistory(imageURL, title, desc);
lightHistories.clear();
lightHistories.add(lightsHistory);

You are calling list.clear() and adding one element to it so the list will have only one element when it's saved to the shared preferences.

0
On

Remove "lightHistories.clear();" If you want to clear arraylist, put It after you use method saveToSharedPrefs().

Regards.