I am trying to retrieve bumps data from my firebase database, then display the bumps with their information that are retrieved from DB.
the problem is my DB contains sub child, and this sub child name's are based on location so I can't retrieve data by sub child name.
so is there a method to retrieve all sub child names? so I can retrieve all data by r retrieve data from sub child ?
this is my DB
this is my code to retrieve the data
private void showbumps() {
ref =FirebaseDatabase.getInstance().getReference().child("SpeedBump");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
List<SpeedBump> bumps = new ArrayList<>();
bumps.clear();
// get bumps info from DB
for (DataSnapshot postSnapshot: snapshot.getChildren()) {
SpeedBump bump = postSnapshot.getValue(SpeedBump.class);
bumps.add(bump);
double bump_lat = bump.getLatitude();
double bump_long = bump.getLongitude();
String bump_type = bump.getType();
String bump_size = bump.getSize();
LatLng latLng = new LatLng(bump_lat,bump_long);
String bump_info = " type : "+ bump_type + " size : "+bump_size ;
// set height & width - apply style
int height = 130;
int width = 130;
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.pin);
Bitmap smallMarker = Bitmap.createScaledBitmap(b, width, height, false);
BitmapDescriptor smallMarkerIcon = BitmapDescriptorFactory.fromBitmap(smallMarker);
// BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.pin);
MarkerOptions marker = new MarkerOptions().position(latLng).title("Bump info").snippet(bump_info).icon(smallMarkerIcon);
// create marker for bumps
mMap.addMarker(marker);
} }
the code doesn't show any bump.
You're almost there. You just need an extra loop, to cater for the extra level of unknown keys in your JSON:
private void showbumps() {
ref =FirebaseDatabase.getInstance().getReference().child("SpeedBump");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
List<SpeedBump> bumps = new ArrayList<>();
bumps.clear();
for (DataSnapshot locationSnapshot: snapshot.getChildren()) {
for (DataSnapshot bumpSnapshot: locationSnapshot.getChildren()) {
SpeedBump bump = bumpSnapshot.getValue(SpeedBump.class);
...
I find it most helpful to give the snapshot variables clear names that indicate what part of the data they capture.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.