更新时间:2022-06-03 03:21:03
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
}
然后您可以将它们用作
public void onDataChange(DataSnapshot dataSnapshot) {
Route route = dataSnapshot.getValue(Route.class);
}
现在,您可以根据需要使用检索到的数据.
Now you can use the data retrieved as you want.
工作代码:
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("***", 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;
}
}
}
这是我正在查询的Firebase节点
This is firebase node I am querying
输出: D/***:55.39,10.33D/堆栈溢出:65.39,13.33D/堆栈溢出:35.39,16.33D/堆栈溢出:85.39,12.33D/堆栈溢出:25.39,17.33D/***:57.39,61.33