Skip to content Skip to sidebar Skip to footer

Android - Retrieving Data From Firebase Database

I am drawing a route between two markers, and I want to save that route. To do that I saved the ArrayList containing the lat and lng in the Firebase database. But I am having probl

Solution 1:

class Route {
    private ArrayList<Location> locations;
    @PropertyName("route")
    public ArrayList<Location> getLocations() {
       return locations;
    }
    @PropertyName("route")
    public void setLocations(ArrayList<Locations> locations) {
        this.locations = locations;
    }
}


class Location{ 
    private Double latitude;
    private Double longitude;

    //getter and setter for lat - long 
}

then you can use them as

public void onDataChange(DataSnapshot dataSnapshot) {
     Route route = dataSnapshot.getValue(Route.class);
}

Now you can use the data retrieved as you want.

Edit: working code:

public class RouteActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.abc);

    FirebaseDatabase.getInstance().getReference("Route").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Route route = dataSnapshot.getValue(Route.class);

            for(Location location : route.getLocations() ) {
                Log.d("stackoverflow", location.getLatitude()+","+location.getLongitude());
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

public static class Route {

    private ArrayList<Location> locations;

    public Route() {

    }

    @PropertyName("route")
    public ArrayList<Location> getLocations() {
        return locations;
    }
    @PropertyName("route")
    public void setLocations(ArrayList<Location> locations) {
        this.locations = locations;
    }
}


public static class Location{
    private Double latitude;
    private Double longitude;

    //getter and setter for lat - long

    public Location() {

    }

    public Double getLatitude() {
        return latitude;
    }

    public void setLatitude(Double latitude) {
        this.latitude = latitude;
    }

    public Double getLongitude() {
        return longitude;
    }

    public void setLongitude(Double longitude) {
        this.longitude = longitude;
    }
}
}

This is firebase node I am querying This is firebase node I am querying

Output: D/stackoverflow: 55.39,10.33 D/stackoverflow: 65.39,13.33 D/stackoverflow: 35.39,16.33 D/stackoverflow: 85.39,12.33 D/stackoverflow: 25.39,17.33 D/stackoverflow: 57.39,61.33


Solution 2:

Make this custom class with values.

class MapRouteCords{
public Double latitude;
public Double longitude;

public MapRouteCords()
{

}

 public MapRouteCords(Double latitude, Double longitude)
{
   this.latitude = latitude;
   this.longitude = longitude;
}

 public Double getLatitude() {
    return latitude;
}

public void setLatitude(Double latitude) {
    this.latitude = latitude;
}

public Double getLongitude() {
    return longitude;
}

public void setLongitude(Double longitude) {
    this.longitude = longitude;
}

Use the method in your OnDataChange Listener

collectcords((Map<String, Object>) dataSnapshot.getValue());

Define the method.

private void collectcords(Map<String, Object> mapcords) {
    ArrayList<Double> alllats  = new ArrayList<Double>();
    ArrayList<Double> alllongs  = new ArrayList<Double>();


    for (Map.Entry<String, Object> entry : mapcords.entrySet()) {
        Map singleGame = (Map) entry.getValue();
        alllats.add((Double) singleGame.get("latitude"));
        alllongs.add((Double) singleGame.get("longitude"));
    }

Solution 3:

Here is a Nice way to get it from database : Follow these steps and at the end you will get what you want.

Step 1 : Create a class

Route

:with all the properties and getters and setters. And make sure to keep a empty constructor.

Step 2 Create an ArrayList of Route and store it in the database using

ref.setValue(arrayListRoutes);

Step 3 Getting data from Firebase Database, Assuming Datasnapshot is the snapshot on ref node. Use this onDataChange(DataSnapshot dataSnapshot):

GenericTypeIndicator<ArrayList<Route>> indicator = new GenericTypeIndicator<ArrayList<Route>>(){};

ArrayList<Route> DataList = dataSnapshot.getValue(indicator);

Post a Comment for "Android - Retrieving Data From Firebase Database"