无法从Firebase中检索数据 [英] Cannot retrieve data from Firebase

查看:153
本文介绍了无法从Firebase中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制两个标记之间的路线,我想保存该路线。为此,我在Firebase数据库中保存了包含lat和lng的ArrayList。但我有问题检索的路标。这是我如何插入:

这是我插入:

  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是学校的名称。我的数据库:

a href =https://i.stack.imgur.com/ujYcQ.png =nofollow noreferrer>



我的POJO类:

  public static class Route {

private ArrayList< Location>位置;

public Route(){
}

public ArrayList< Location> getLocations(){
返回位置;


public void setLocations(ArrayList< Location> locations){
this.locations = locations;
}
}


公共静态类位置{
private Double latitude;
私人双倍经度;
$ b 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;


$ / code $ / pre
$ b $ p

检索航点:$ b​​
$ b

  points = new ArrayList(); 
userRef.child(sharedPreferences.getString(school,null))。child(routes)。child(sh.getString(key,null))。addListenerForSingleValueEvent(new ValueEventListener(){
@Override $ b $ 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);






$ b

但是我得到一个数据库异常:


$ b


DatabaseException:无法将java.util.ArrayList类型的对象转换为packagename类型

我不知道为什么

解决方案

你的代码几乎是正确的,但我认为它是保存的数据让它失望。当您从数据库中加载数据时,您将数据编组为 Route 实例:

  Route route = dataSnapshot.getValue(Route.class); 

这将会在每个下面有一个位置节点child在你的POJO类中匹配 setLocations(ArrayList< Location>)。相反,它所检索的数据直接是 ArrayList



保存位置列表时,您需要按照 $ schoolName / routes / $ routeId / locations 存储地点列表:

所以,保存路线指向数据时,您需要执行以下操作:

$ b b $ b

  database.child(schoolName).child(routes)。child(routeId).child(locations)。setValue(points); 

一个简单的想法是:如果 Route 也有一个名称字段( setName(String)),那么数据库也可能有 $ schoolName / routes / $ routeId / name 与位置值相同。

或者,数据库,你可以转换你的变量到 Route 实例,然后直接把它推到 $ schoolName / routes / $ routeId 代替,这将确保它被正确存储。


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:

This is how I am inserting:

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

解决方案

Your code is almost right, but I think it's the saving of the data that's letting it down. When you load the data back from the database you're marshalling the data into a Route instance:

Route route = dataSnapshot.getValue(Route.class);

This will expect a locations node under each child to match setLocations(ArrayList<Location>) in your POJO class. Instead, the data it retrieves is the ArrayList directly.

When saving your location list, you'll need to arrange the structure such that the list of locations are stored under $schoolName/routes/$routeId/locations:

So, when saving your route point data, you'll need to do:

database.child(schoolName).child("routes").child(routeId).child("locations").setValue(points);

An easy way to think of this would be: if the Route also had a name field (setName(String)) then the database may also have $schoolName/routes/$routeId/name in the same place as the locations value.

Alternatively, when saving to the database, you could convert your points variable to a Route instance and then push that directly to $schoolName/routes/$routeId instead, which will ensure that it's stored correctly.

这篇关于无法从Firebase中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆