Android - Retrieving Data From Firebase Database
Solution 1:
classRoute{
private ArrayList<Location> locations;
@PropertyName("route")public ArrayList<Location> getLocations() {
return locations;
}
@PropertyName("route")public void setLocations(ArrayList<Locations> locations) {
this.locations = locations;
}
}
classLocation{
privateDouble latitude;
privateDouble longitude;
//getter and setter for lat - long
}
then you can use them as
publicvoidonDataChange(DataSnapshot dataSnapshot){
Route route = dataSnapshot.getValue(Route.class);
}
Now you can use the data retrieved as you want.
Edit: working code:
publicclassRouteActivityextendsAppCompatActivity {
@OverrideprotectedvoidonCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.abc);
FirebaseDatabase.getInstance().getReference("Route").addListenerForSingleValueEvent(newValueEventListener() {
@OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
Route route = dataSnapshot.getValue(Route.class);
for(Location location : route.getLocations() ) {
Log.d("stackoverflow", location.getLatitude()+","+location.getLongitude());
}
}
@OverridepublicvoidonCancelled(DatabaseError databaseError) {
}
});
}
publicstaticclassRoute {
privateArrayList<Location> locations;
publicRoute() {
}
@PropertyName("route")
publicArrayList<Location> getLocations() {
return locations;
}
@PropertyName("route")
publicvoidsetLocations(ArrayList<Location> locations) {
this.locations = locations;
}
}
publicstaticclassLocation{
privateDouble latitude;
privateDouble longitude;
//getter and setter for lat - longpublicLocation() {
}
publicDoublegetLatitude() {
return latitude;
}
publicvoidsetLatitude(Double latitude) {
this.latitude = latitude;
}
publicDoublegetLongitude() {
return longitude;
}
publicvoidsetLongitude(Double longitude) {
this.longitude = longitude;
}
}
}
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.
classMapRouteCords{
publicDouble latitude;
publicDouble longitude;
public MapRouteCords()
{
}
public MapRouteCords(Double latitude, Double longitude)
{
this.latitude = latitude;
this.longitude = longitude;
}
publicDouble getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
publicDouble 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.
privatevoid 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"