-2

enter image description here

How do I parse through this database and get all the double values. Help me

Pemba Tamang
  • 1,235
  • 16
  • 38

3 Answers3

1

Assuming that the DRIVERID is a direct child of your Firebase root, to get those values, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference yourRef = rootRef.child("DRIVERID").child("LatLng");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        double Lat = dataSnapshot.child("Lat").getValue(Double.class);
        double Lng = dataSnapshot.child("Lng").getValue(Double.class);
        Log.d("TAG", Lat + " / " + Lng);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
yourRef.addListenerForSingleValueEvent(eventListener);

The output will be:

22.582798 / 88.35998

To get also the Compass, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference yourRef = rootRef.child("DRIVERID");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        double Compasss =  = dataSnapshot.child("Compasss").getValue(Double.class);
        double Lat = dataSnapshot.child("LatLng").child("Lat").getValue(Double.class);
        double Lng = dataSnapshot.child("LatLng").child("Lng").getValue(Double.class);
        Log.d("TAG", Compasss + " / " + Lat + " / " + Lng);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
yourRef.addListenerForSingleValueEvent(eventListener);

The output will be:

54.5750122070312 / 22.582798 / 88.35998

The line of code that worked for the OP is:

HashMap Compasss = (HashMap) dataSnapshot.getValue(Boolean.parseBoolean("Compasss"));
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks alex ..this works.....what about the compass values?? – Pemba Tamang Jan 10 '18 at 17:08
  • Got an error `com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Double to String` on this line: `String Lat = dataSnapshot.child("LatLng").child("Lat").getValue(String.class);` – Pemba Tamang Jan 10 '18 at 17:24
  • Please see my updated answer. Does it work now? – Alex Mamo Jan 10 '18 at 17:27
  • I am logging only the compass values that too is null....I am running your updated code – Pemba Tamang Jan 10 '18 at 17:27
  • Sorry, my mistake. Just removed `.child("LatLng")`. See again please. It's ok now? – Alex Mamo Jan 10 '18 at 17:29
  • nope it is not working.... – Pemba Tamang Jan 10 '18 at 17:29
  • Changed `long` to `double`. Sorry but i'm wrtting from my phone. Does it work now? – Alex Mamo Jan 10 '18 at 17:31
  • `String Compasss = dataSnapshot.child("Compasss").getValue(String.class); double Lat = (double) dataSnapshot.child("LatLng").child("Lat").getValue(); double Lng = (double) dataSnapshot.child("LatLng").child("Lng").getValue(); Log.d("TAG", Compasss + " / " + Lat + " / " + Lng);` lat and lon are coming but compass is still null Log: `D/TAG: null / 22.5826983 / 88.3589983` – Pemba Tamang Jan 10 '18 at 17:32
  • because you still need to change this line `String Compasss = dataSnapshot.child("Compasss").getValue(String.class);` with `double Compasss = (double) dataSnapshot.child("Compasss").getValue();` – Alex Mamo Jan 10 '18 at 17:34
  • What about now? – Alex Mamo Jan 10 '18 at 17:34
  • it's causing an error `java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference` maybe because compass does not have a child – Pemba Tamang Jan 10 '18 at 17:36
  • Try to use `.getValue(Double.class)` and not `(double)`. See my exact code o have provided you. – Alex Mamo Jan 10 '18 at 17:40
  • still the virtual method on a null object error alex – Pemba Tamang Jan 10 '18 at 17:44
  • Please add a screenshot of your code and a new screenshot of your database that includes also the root. – Alex Mamo Jan 10 '18 at 17:45
  • `HashMap Compasss = (HashMap) dataSnapshot.getValue(Boolean.parseBoolean("Compasss"));` `Compasss.get("Compass");` worked!! anyways thanks for your help alex...update your code there...I will upvote as soon as I get 15 rep. – Pemba Tamang Jan 10 '18 at 17:52
  • Just edited my answer again. Is something strange with this line but glad it worked for you. Cheers! – Alex Mamo Jan 10 '18 at 17:54
  • Cheers alex!....try that if you get time and please do explain the code because I just pressed alt+enter and it worked. lol – Pemba Tamang Jan 10 '18 at 18:14
0

querying firebase with named node like DRIVERID in your case will be passing the child name after getting the refernce to DRIVERID node.

firebaseRefTODRIVERID.child("Latlng").once("value", snap => {
      console.log(snap.val())
})

you can check this answer

Himanshu
  • 12,071
  • 7
  • 46
  • 61
  • I'm a newbie so I hope I make some sense... I am using java in android studio...from what I found I'm supposed to do something like this.. `for (DataSnapshot snapshot : dataSnapshot.getChildren()) { lat = (double) snapshot.child("Lat").getValue(); lon = (double) snapshot.child("Lon").getValue(); compass = (double) snapshot.child("Compass").getValue(); }` but obviously the compass value needs to be parsed differently...I don't know how – Pemba Tamang Jan 10 '18 at 17:00
0

I recommend creating 2 Java classes:

  • Location(double Lat, double Lng);
  • Driver(String Compass, Location LatLng)

Then you'd be able to get this using:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("DRIVERID");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Driver driver = datasnapshot.getValue(Driver.class);
        //your data would be stored in this driver variable
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
rootRef.addListenerForSingleValueEvent(eventListener);