I want to save Hashset
Object in to Sharedpreference
and than want retrieve that data. I am storing data in to hashset
and and converting object in to json
using Gson
. Actually m storing bitmap in to Hashset
. I am able to convert and save Hashsetobject
into sharedpreference
. I am getting problem when I am retrieving and converting json
to Hashset
Object.
HashSet<images> img = new HashSet<images>(CIRCLES_LIMIT);
Here is Method For Saving Object
in to Sharedpreference
.
public void saveString() throws JSONException {
Object spSquare = c.getStringDrawObjImages();
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = sharedPrefs.edit();
Gson gson = new Gson();
String jsonSquare = gson.toJson(spSquare)
editor.putString("kEySquare", jsonSquare);
editor.commit();
}
Method For Retrieving That Object.
public void openString() {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
Gson gson = new Gson();
String jsonSquare=sharedPrefs.getString("kEySquare",null);
Type typeSquare = new TypeToken<HashSet<images>>(){}.getType();
HashSet<images> arrayListSquare = gson.fromJson(jsonSquare,typeSquare);`//getting Exception here jsonSyntax Error`
if (arrayListSquare != null) {
img = arrayListSquare;
}
}