1

How do I retrieve the firebase list in the same order as it is added. Looks like I’m getting them in a random order. I need to have them in order to draw my polyline in correct order.

List<LatLng> latLngList = new ArrayList<>();
ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        for (DataSnapshot direction : snapshot.getChildren()) {
            if (direction.getKey().equals(firebaseKey)) {
                MyLatLng wantedDirection = direction.getValue(MyLatLng.class);
                for (Waypoints waypoint : wantedDirection.getWaypoints().values()) {
                    double lat = waypoint.getLatitude();
                    double lang = waypoint.getLongitude();
                    LatLng latLng = new LatLng(lat, lang);
                    latLngList.add(latLng);
             }

I want the JSON file to be in the same order as it the entries were added to the firebase list:

{
  "timeStamp" : "2016-05-03 23:06:05",
  "waypoints" : {
    "-KGsf4xB_rZbsAMW4I2D" : {
      "latitude" : 58.338713,
      "longitude" : 11.90885
    },
    "-KGsf5M9YtZn6Yq22Kts" : {
      "latitude" : 58.339218,
      "longitude" : 11.910351
    },
    "-KGsf5qSV3X7cAIBtANF" : {
      "latitude" : 58.340572,
      "longitude" : 11.915417
    },
    "-KGsf6oww79POAY_e7kf" : {
      "latitude" : 58.342271,
      "longitude" : 11.921562
    },
    "-KGsf7JBaac5o7VMEZMh" : {
      "latitude" : 58.344006,
      "longitude" : 11.926909
    },
    "-KGsf7nGpMEgPA0j1rRl" : {
      "latitude" : 58.345594,
      "longitude" : 11.929302
    },
    "-KGsf8HjFtn7Dxpx-DpO" : {
      "latitude" : 58.347846,
      "longitude" : 11.931577
    },
    "-KGsf8ltvEBeqXE_me8Z" : {
      "latitude" : 58.350397,
      "longitude" : 11.932334
    }
  }
}
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Rasmus Rajje Josefsson
  • 1,564
  • 2
  • 15
  • 33

2 Answers2

0

DataSnapshot.getChildren() will return the children in the order in which you've queried them from the database. If you're not specifying orderByChild() or orderByPriority() explicitly, that order will be by key, which means they will be iterated in the order they were inserted.

But for the waypoints, you are defining your own order:

for (Waypoints waypoint : wantedDirection.getWaypoints().values()

The values() of a Map are not going to be in a specific order afaik.

Update

To see the order in which the items are retrieved from Firebase, you can traverse the DataSnapshot:

public void onDataChange(DataSnapshot snapshot) {
  for (DataSnapshot direction: snapshot.getChildren()) {
    DataSnapshot waypoints = direction.child("waypoints");
    for (Waypoints waypoint: waypoints.getChildren()) {
        double lat = waypoint.child("latitude").getValue(Double.class);
        double lon = waypoint.child("longitude").getValue(Double.class);
        LatLng latLng = new LatLng(lat, long);
        latLngList.add(latLng);
    }
}

Alternatively, you can look for a map that returns its entries in the order of its keys.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

This worked for me:

 for (Map.Entry<String, Waypoints> waypoint : wantedDirection.getWaypoints().entrySet()) {
                        String key = waypoint.getKey();
                        Waypoints value = waypoint.getValue();

                        double lat = value.getLatitude();
                        double lang = value.getLongitude();
                        LatLng latLng = new LatLng(lat, lang);
                        latLngRealList.add(latLng);
                    }
Rasmus Rajje Josefsson
  • 1,564
  • 2
  • 15
  • 33