I just discovered TinyDB which seems quite useful to handle object storage into the SharedPreferences.
I'm almost there but I'm getting an error: Forgot to register a type adapter? Note that the GSON library from Google is used together with TinyDB.
So, let me share my code with you. Any help would be highly appreciated.
Here is the stackTrace
java.lang.UnsupportedOperationException: Attempted to serialize
java.lang.Class: com.ilepez.weatherapp.data.model.CityArrayList. Forgot to register a type adapter?
at com.google.gson.internal.bind.TypeAdapters$1.write(TypeAdapters.java:76)
at com.google.gson.internal.bind.TypeAdapters$1.write(TypeAdapters.java:69)
at com.google.gson.Gson.toJson(Gson.java:669)
at com.google.gson.Gson.toJson(Gson.java:648)
at com.google.gson.Gson.toJson(Gson.java:603)
at com.google.gson.Gson.toJson(Gson.java:583)
at com.ilepez.weatherapp.utils.TinyDB.putObject(TinyDB.java:462)
at com.ilepez.weatherapp.activity.SearchActivity.onItemClick(SearchActivity.java:67)
at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:928)
at android.widget.AutoCompleteTextView.access$500(AutoCompleteTextView.java:93)
at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1230)
at android.widget.AdapterView.performItemClick(AdapterView.java:345)
at android.widget.AbsListView.performItemClick(AbsListView.java:1547)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3821)
at android.widget.AbsListView$3.run(AbsListView.java:5841)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
07-17 10:55:26.283 15392-15392/com.ilepez.weatherapp E/libEGL: call to OpenGL ES API with no current context (logged once per thread)
So, let me share my code with you. Any help would be highly appreciated.
So here is the moment when I add my object to TinyDB
City city = new City(description, itemLatitude, itemLongitude);
ArrayList<Object> savedCity = new ArrayList<>();
savedCity.add(city);
for (City oCity : CityArrayList.getInstance().getCityList()) {
savedCity.add(oCity);
}
TinyDB tinydb = new TinyDB(this);
tinydb.putObject("city", CityArrayList.class);
Here is my model
public class CityArrayList {
public ArrayList<City> cityList;
private CityArrayList() {
cityList = new ArrayList<City>();
}
public ArrayList<City> getCityList() {
return cityList;
}
private static CityArrayList instance;
public static CityArrayList getInstance() {
if (instance == null) instance = new CityArrayList();
return instance;
}
}
And for the city itself
public class City {
private String cityName;
private double cityLat;
private double cityLong;
public City(String cityName, double cityLat, double cityLong) {
this.cityName = cityName;
this.cityLat = cityLat;
this.cityLong = cityLong;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public double getCityLat() {
return cityLat;
}
public void setCityLat(double cityLat) {
this.cityLat = cityLat;
}
public double getCityLong() {
return cityLong;
}
public void setCityLong(double cityLong) {
this.cityLong = cityLong;
}
@Override
public String toString() {
return "City{" +
"cityName='" + cityName + '\'' +
", cityLat=" + cityLat +
", cityLong=" + cityLong +
'}';
}
}
You are passing in the wrong thing.
the putObject
method requires you to pass in a String key and an already intantiated object, not a class type.
here is what your code should be to work correctly:
City mCity = new City(description, itemLatitude, itemLongitude);
tinydb.putObject("city", mCity);