I am drawing a route between two markers, and I want to save that route. To do that I saved the ArrayList
containing the lat and lng in the Firebase database. But I am having problems retrieving the waypoints. This is how I inserted:
String routeId = database.push().getKey();
database.child(sharedPreferences.getString("school", null)).child("routes").child(routeId).setValue(points);
SharedPreferences.Editor editor = getActivity().getSharedPreferences("sh", MODE_PRIVATE).edit();
editor.putString("key", routeId);
editor.commit();
Where the sharedPreferences
string "school" is the name of the school, routeId
is a push()
key and the points is an ArrayList
of waypoints.
My database:
My POJO classes:
public static class Route {
private ArrayList<Location> locations;
public Route() {
}
public ArrayList<Location> getLocations() {
return locations;
}
public void setLocations(ArrayList<Location> locations) {
this.locations = locations;
}
}
public static class Location {
private Double latitude;
private Double longitude;
public Location() {
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
}
Retrieval of waypoints:
points = new ArrayList();
userRef.child(sharedPreferences.getString("school", null)).child("routes").child(sh.getString("key",null)).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Route route = dataSnapshot.getValue(Route.class);
for (Location location : route.getLocations()) {
double lat = location.getLatitude();
double lng = location.getLongitude();
position = new LatLng(lat, lng);
points.add(position);
}
}
But I am getting a database exception:
DatabaseException: Can't convert object of type java.util.ArrayList to type packagename
I don't know why.