You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using this method to save an object of a class wrapping a HashMap object is resulting in a LinkTreeMap Class casting error.
Eg Class:
public class Categories {
private String categoryName;
private ActivityMasterModel amm;
@SerializedName("activities")
@Expose
private ArrayList<ActivityMasterModel> allActivities;
private HashMap<String, ArrayList<ActivityMasterModel>> allActivitiesByCategory;
public ArrayList<ActivityMasterModel> getAllActivities() {
return allActivities;
}
public void setAllActivities(ArrayList<ActivityMasterModel> allActivities) {
this.allActivities = allActivities;
}
I couldnt save allActivitiesByCategory object into shared preferences using putObject method. Hence I created the above class to be used with putObject method. However, the object allActivitiesByCategory is saved using the below modified method.
public void putHashMapObject(String key, Object obj) {
checkForNullKey(key);
Type type = new TypeToken<Map<String, ArrayList<ActivityMasterModel>>>() {}.getType();
Gson gson = new Gson();
putString(key, gson.toJson(obj, type));
}
However, the same solution didnt work for SparseArray instead of HashMap. I couldnt solve the issue yet. I am not a Java guy, I should say a beginner, and trying to get my head around it.
The text was updated successfully, but these errors were encountered:
I am happy you were able to find a hint online that enabled you to save the HashMap.
SparseArray is a little more complicated for Gson which TinyDB uses for saving Objects. To save a SparseArray, you'd have to create a custom put object method based on this example here. https://stackoverflow.com/a/18644836/1463931
Using this method to save an object of a class wrapping a HashMap object is resulting in a LinkTreeMap Class casting error.
Eg Class:
I couldnt save
allActivitiesByCategory
object into shared preferences using putObject method. Hence I created the above class to be used with putObject method. However, the objectallActivitiesByCategory
is saved using the below modified method.I was following the below link and it helped a lot.
https://stackoverflow.com/questions/32444863/google-gson-linkedtreemap-class-cast-to-myclass
However, the same solution didnt work for SparseArray instead of HashMap. I couldnt solve the issue yet. I am not a Java guy, I should say a beginner, and trying to get my head around it.
The text was updated successfully, but these errors were encountered: