且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检索Firebase中一键保存的“双”值?

更新时间:2023-02-23 08:18:12

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

相关阅读

技术问答最新文章