【发布时间】:2018-04-06 06:02:21
【问题描述】:
我正在两个标记之间绘制一条路线,我想保存该路线。为此,我将包含 lat 和 lng 的 ArrayList 保存在 Firebase 数据库中。但我在检索航点时遇到问题。我是这样插入的:
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();
sharedPreferences 字符串“school”是学校的名称,routeId 是 push() 键,points 是航点的 ArrayList。
我的数据库:
我的 POJO 课程:
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;
}
}
路点检索:
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);
}
}
但是我得到一个数据库异常:
DatabaseException:无法将 java.util.ArrayList 类型的对象转换为 packagename 类型
我不知道为什么。
【问题讨论】:
-
我尝试使用链接中显示的 getAllChildren 方法,但它似乎不起作用。
标签: android firebase firebase-realtime-database