I have a class that all data stored. I use ArrayList<ModelHomeListing> to process it. I need to store it in SharedPreferences but I cannot use it without Gson because there are a lot of type of variables in the model.
I use the code below while storing:
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(listings_items, new TypeToken<ArrayList<ModelHomeListing>>() {}.getType());
editor.putString("listing_items", json);
editor.commit();
...and I fetch it with below:
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Gson gson = new Gson();
String json = sharedPrefs.getString("listing_items", null);
Type type = new TypeToken<ArrayList<ModelHomeListing>>() {}.getType();
items = gson.fromJson(json, type);
However I get StackOverflowError if I use gson version 2.3.1. If I change the version to 1.7.1 on gradle, I get this error:
java.lang.IllegalStateException: circular reference error
Offending field: adapter
Offending object: preserveType: false, type: class...
Which version should I use and how can I prevent this error?
ModelHomeListing class:
public class ModelHomeListing {
private Context context;
private int id;
private String title;
private String title_slug;
private String description;
// location
private String location_country;
private String location_city;
private String location_district;
private String location_neighborhood;
private double location_latitude;
private double location_longitude;
private ArrayList<String> location_vicinities;
// properties
private String properties_home_property_type;
private int properties_home_number_of_rooms;
private int properties_home_living_rooms;
private int properties_home_bathrooms;
private int properties_square_meter;
private String properties_building_age;
private int properties_number_of_floors;
private String properties_home_floor_number;
private String properties_building_condition;
private int properties_monthly_fee;
private String properties_home_usage_condition;
private String properties_heating;
private String properties_fuel_type;
private ArrayList<String> properties_home_inside_features;
private ArrayList<String> properties_home_outside_features;
private int properties_rental_income;
private String properties_applicable_to_loan;
// price
private int price_amount;
private String price_currency;
private String category;
private int created_at;
private int updated_at;
private String sale_rent;
private ArrayList<String> photos_thumbnail;
private ArrayList<String> photos_middle;
private ArrayList<String> photos_large;
private String status;
private int view_count;
private boolean doping;
private boolean favorite;
private SharedPreferences sp;
private SharedPreferences.Editor editor;
public ModelHomeListing(Context context) {
this.context = context;
id = 0;
title = "";
title_slug = "";
description = "";
location_country = "";
location_city = "";
location_district = "";
location_neighborhood = "";
location_latitude = 0.0;
location_longitude = 0.0;
location_vicinities = new ArrayList<String>();
properties_home_property_type = "";
properties_home_number_of_rooms = 0;
properties_home_living_rooms = 0;
properties_home_bathrooms = 0;
properties_square_meter = 0;
properties_building_age = "";
properties_number_of_floors = 0;
properties_home_floor_number = "";
properties_building_condition = "";
properties_monthly_fee = 0;
properties_home_usage_condition = "";
properties_heating = "";
properties_fuel_type = "";
properties_home_inside_features = new ArrayList<String>();
properties_home_outside_features = new ArrayList<String>();
properties_rental_income = 0;
properties_applicable_to_loan = "";
price_amount = 0;
price_currency = "";
category = "";
created_at = 0;
updated_at = 0;
sale_rent = "";
photos_thumbnail = new ArrayList<String>();
photos_middle = new ArrayList<String>();
photos_large = new ArrayList<String>();
status = "";
view_count = 0;
doping = false;
favorite = false;
sp = PreferenceManager.getDefaultSharedPreferences(context);
editor = sp.edit();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle_slug() {
return title_slug;
}
public void setTitle_slug(String title_slug) {
this.title_slug = title_slug;
}
public String getLocation_city() {
return location_city;
}
public void setLocation_city(String location_city) {
this.location_city = location_city;
}
public String getSale_rent() {
return sale_rent;
}
public void setSale_rent(String sale_rent) {
this.sale_rent = sale_rent;
}
public int getCreated_at() {
return created_at;
}
public void setCreated_at(int created_at) {
this.created_at = created_at;
}
public String getLocation_district() {
return location_district;
}
public void setLocation_district(String location_district) {
this.location_district = location_district;
}
public String getLocation_neighborhood() {
return location_neighborhood;
}
public void setLocation_neighborhood(String location_neighborhood) {
this.location_neighborhood = location_neighborhood;
}
public ArrayList<String> getLocation_vicinities() {
return location_vicinities;
}
public void setLocation_vicinities(ArrayList<String> location_vicinities) {
this.location_vicinities = location_vicinities;
}
public String getProperties_home_property_type() {
return properties_home_property_type;
}
public void setProperties_home_property_type(String properties_home_property_type) {
this.properties_home_property_type = properties_home_property_type;
}
public int getProperties_home_number_of_rooms() {
return properties_home_number_of_rooms;
}
public void setProperties_home_number_of_rooms(int properties_home_number_of_rooms) {
this.properties_home_number_of_rooms = properties_home_number_of_rooms;
}
public int getProperties_square_meter() {
return properties_square_meter;
}
public void setProperties_square_meter(int properties_square_meter) {
this.properties_square_meter = properties_square_meter;
}
public int getProperties_number_of_floors() {
return properties_number_of_floors;
}
public void setProperties_number_of_floors(int properties_number_of_floors) {
this.properties_number_of_floors = properties_number_of_floors;
}
public int getPrice_amount() {
return price_amount;
}
public void setPrice_amount(int price_amount) {
this.price_amount = price_amount;
}
public String getPrice_currency() {
return price_currency;
}
public void setPrice_currency(String price_currency) {
this.price_currency = price_currency;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public boolean isFavorite() {
return sp.getBoolean("listing" + String.valueOf(this.id), false);
}
public void setFavorite(boolean favorite) {
editor.putBoolean("listing" + String.valueOf(this.id), favorite);
editor.commit();
this.favorite = favorite;
}
public ArrayList<String> getPhotos_thumbnail() {
return photos_thumbnail;
}
public void setPhotos_thumbnail(ArrayList<String> photos_thumbnail) {
this.photos_thumbnail = photos_thumbnail;
}
public ArrayList<String> getPhotos_middle() {
return photos_middle;
}
public void setPhotos_middle(ArrayList<String> photos_middle) {
this.photos_middle = photos_middle;
}
public ArrayList<String> getPhotos_large() {
return photos_large;
}
public void setPhotos_large(ArrayList<String> photos_large) {
this.photos_large = photos_large;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLocation_country() {
return location_country;
}
public void setLocation_country(String location_country) {
this.location_country = location_country;
}
public double getLocation_latitude() {
return location_latitude;
}
public void setLocation_latitude(double location_latitude) {
this.location_latitude = location_latitude;
}
public double getLocation_longitude() {
return location_longitude;
}
public void setLocation_longitude(double location_longitude) {
this.location_longitude = location_longitude;
}
public int getProperties_home_living_rooms() {
return properties_home_living_rooms;
}
public void setProperties_home_living_rooms(int properties_home_living_rooms) {
this.properties_home_living_rooms = properties_home_living_rooms;
}
public int getProperties_home_bathrooms() {
return properties_home_bathrooms;
}
public void setProperties_home_bathrooms(int properties_home_bathrooms) {
this.properties_home_bathrooms = properties_home_bathrooms;
}
public String getProperties_building_age() {
return properties_building_age;
}
public void setProperties_building_age(String properties_building_age) {
this.properties_building_age = properties_building_age;
}
public String getProperties_home_floor_number() {
return properties_home_floor_number;
}
public void setProperties_home_floor_number(String properties_home_floor_number) {
this.properties_home_floor_number = properties_home_floor_number;
}
public String getProperties_building_condition() {
return properties_building_condition;
}
public void setProperties_building_condition(String properties_building_condition) {
this.properties_building_condition = properties_building_condition;
}
public int getProperties_monthly_fee() {
return properties_monthly_fee;
}
public void setProperties_monthly_fee(int properties_monthly_fee) {
this.properties_monthly_fee = properties_monthly_fee;
}
public String getProperties_home_usage_condition() {
return properties_home_usage_condition;
}
public void setProperties_home_usage_condition(String properties_home_usage_condition) {
this.properties_home_usage_condition = properties_home_usage_condition;
}
public String getProperties_heating() {
return properties_heating;
}
public void setProperties_heating(String properties_heating) {
this.properties_heating = properties_heating;
}
public String getProperties_fuel_type() {
return properties_fuel_type;
}
public void setProperties_fuel_type(String properties_fuel_type) {
this.properties_fuel_type = properties_fuel_type;
}
public ArrayList<String> getProperties_home_inside_features() {
return properties_home_inside_features;
}
public void setProperties_home_inside_features(ArrayList<String> properties_home_inside_features) {
this.properties_home_inside_features = properties_home_inside_features;
}
public ArrayList<String> getProperties_home_outside_features() {
return properties_home_outside_features;
}
public void setProperties_home_outside_features(ArrayList<String> properties_home_outside_features) {
this.properties_home_outside_features = properties_home_outside_features;
}
public int getProperties_rental_income() {
return properties_rental_income;
}
public void setProperties_rental_income(int properties_rental_income) {
this.properties_rental_income = properties_rental_income;
}
public String getProperties_applicable_to_loan() {
return properties_applicable_to_loan;
}
public void setProperties_applicable_to_loan(String properties_applicable_to_loan) {
this.properties_applicable_to_loan = properties_applicable_to_loan;
}
public int getUpdated_at() {
return updated_at;
}
public void setUpdated_at(int updated_at) {
this.updated_at = updated_at;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getView_count() {
return view_count;
}
public void setView_count(int view_count) {
this.view_count = view_count;
}
public boolean isDoping() {
return doping;
}
public void setDoping(boolean doping) {
this.doping = doping;
}
}
adapterin ModelHomeListing class. - halilkayaSharedPreferences,SharedPreferences.Editor, andContextmembers are most likely causing your problem. - Mike M.