如何检索Firebase中一键保存的“双”值? [英] How to retrieve 'double' values saved under one key in Firebase?

查看:148
本文介绍了如何检索Firebase中一键保存的“双”值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在数据库中存储了 Geofire 坐标,现在我试图使用 childEventListener()这个:

  mDatabase.child(geofire)。child(key).child(l)。addChildEventListener(new ChildEventListener(){
@Override
public void onChildAdded(DataSnapshot dataSnapshot,String s){$ b $ if(dataSnapshot.getValue()!= null){
//线上错误(Double&Double,Double>)dataSnapshot.getValue();
venueLatString = newRequest.get(0).toString();
venueLngString = newRequest.get(1).toSt环();
Toast.makeText(getBaseContext(),dataSnapshot.getValue()。toString(),Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getBaseContext(),NULLL,Toast.LENGTH_SHORT).show();
}
}
...
...
});

Geofire coordnates存储在数据库中,如下所示:

java.lang.ClassCastException:java .lang.Double不能转换成上面指定的java.util.Map 。



这里出了什么问题

strong>为什么我得到这个错误?



请让我知道。

解决方案

如果使用 addChildEventListener ,则产生的 DataSnapshot 来自被引用节点的子节点。所以在你的情况下,你的ref节点是 l ,然后在 onChildAdded 中, dataSnapshot 是:


  • 0 28.8 ...


  • 1 78.7 ...




c $ c $> onChidAdded()将被调用两次。

您可以根据该逻辑修改代码或使用 addValueEventListener 来代替。如果您使用 addValueEventListener ,您的代码将如下所示:

  mDatabase.child(geofire)。child(key).child(l)。addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
如果(dataSnapshot.getValue()!= null){
Double d1 =(Double)dataSnapshot.child(0)。getValue(); $ b $ Double d2 =(Double)dataSnapshot.child 1)。getValue();

venueLatString = String.valueOf(d1);
venueLngString = String.valueOf(d2);
} else {
Toast .makeText(getBaseContext(),NULLL,Toast.LENGTH_SHORT).show();
}
}

...
});

希望这可以帮助您

I have stored Geofire coordinates in database and now I'm trying to retrieve them using childEventListener() like this:

    mDatabase.child("geofire").child(key).child("l").addChildEventListener(new ChildEventListener() {
                                                @Override
                                                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                                                    if (dataSnapshot.getValue() != null) {
                                                        //error on line below
                                                        Map<Double, Double> newRequest = (Map<Double, Double>) dataSnapshot.getValue();
                                                        venueLatString = newRequest.get(0).toString();
                                                        venueLngString = newRequest.get(1).toString();
                                                        Toast.makeText(getBaseContext(), dataSnapshot.getValue().toString(), Toast.LENGTH_SHORT).show();
                                                    } else {
                                                        Toast.makeText(getBaseContext(), "NULLL", Toast.LENGTH_SHORT).show();
                                                    }
                                                }
                                                ...
                                                ...
                                            });

Geofire coordnates are stored in database like this:

I'm getting this error: java.lang.ClassCastException: java.lang.Double cannot be cast to java.util.Map on line specified above.

What is going wrong here and why am I getting this error?

Please let me know.

解决方案

If you use addChildEventListener, the DataSnapshot produced is from the child node of the referenced node. So in your case, your ref node is l, then inside onChildAdded, the dataSnapshot is :

  • key 0 with value 28.8...

  • key 1 with value 78.7...

So the onChidAdded() will be called twice.

You can modify your code based on that logic or use addValueEventListener instead. And if you use addValueEventListener, your code will be like this:

mDatabase.child("geofire").child(key).child("l").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.getValue() != null) {
            Double d1 = (Double) dataSnapshot.child("0").getValue();
            Double d2 = (Double) dataSnapshot.child("1").getValue();

            venueLatString = String.valueOf(d1);
            venueLngString = String.valueOf(d2);
        } else {
            Toast.makeText(getBaseContext(), "NULLL", Toast.LENGTH_SHORT).show();
        }
    }

    ...
});

Hope this helps

这篇关于如何检索Firebase中一键保存的“双”值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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