How to retrieve 'double' values saved under one key in Firebase?

故事扮演 提交于 2019-12-30 14:36:30

问题


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.


回答1:


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




回答2:


Can you please Try Following.

final HashMap<String, Object> taskMap = (HashMap<String, Object>) objectMap.getValue();
                        double lat = Double.parseDouble(taskMap.get("0"));
                        double lng = Double.parseDouble(taskMap.get("1"));



回答3:


mDatabase.child("geofire").child(key).child("l").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                double latati = 0, longit = 0;
                for (DataSnapshot child : dataSnapshot.getChildren()) {
//                    Log.e("!! All Data ::> ", child.getKey()+" "+child.getValue());
                    if (child.getKey().equalsIgnoreCase("0")) {
                        latati = Double.parseDouble(String.valueOf(child.getValue()));
                    }
                    if (child.getKey().equalsIgnoreCase("1")) {
                        longit = Double.parseDouble(String.valueOf(child.getValue()));
                    }
                }
                Log.e("!_@@__Lat Long ::", latati + " " + longit);
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
                Log.e("!!__: ", "onCancelled ", firebaseError.toException());
            }
        });


来源:https://stackoverflow.com/questions/41691463/how-to-retrieve-double-values-saved-under-one-key-in-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!