0

As shown in the picture I have campaign branch which is a dynamic list where I save every campaigns data.

https://drive.google.com/file/d/16xe1aeAxEbtfiiUFoXtlqvFPucHejQp7/view?usp=sharing

Every campaign data includes location which is an other branch inside the campaign, I want to get location data of all campaigns. I tried it using the code below but it return null value.

void getCampaignsLocations(final OnGetDataListener onGetDataListener){
        onGetDataListener.onStart();
        Toast.makeText(this, "started", Toast.LENGTH_SHORT).show();
        DatabaseReference campaignsRoot = FirebaseDatabase.getInstance().getReference();
        DatabaseReference campaignsPath = campaignsRoot.child("campaigns");
        ValueEventListener valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot ds: dataSnapshot.getChildren()){
                    LatLng l = ds.child("location").getValue(LatLng.class);
                    campaignsLocations.add(l);
                    onGetDataListener.onSuccess(dataSnapshot,0,"");
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        };
        campaignsPath.addListenerForSingleValueEvent(valueEventListener);
    }
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Ahmed El Sayed
  • 499
  • 1
  • 8
  • 17
  • it's built in in google play map, i also tried to do this but it didn't work String latitude = String.valueOf(ds.child("location").child("latitude").getValue(Double.class)); but it returns null i think the problem with calling the path – Ahmed El Sayed May 14 '19 at 17:10

1 Answers1

1

To solve this, please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference campaignsRef = rootRef.child("campaigns");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            double latitude = ds.child("location").child("latitude").getValue(Double.class);
            double longitute = ds.child("location").child("longitute").getValue(Double.class);
            Log.d(TAG, latitude ", " + longitute);
            LatLng latLng = new LatLng(latitude, longitute);
            //Do what you need to do with your LatLng object
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
campaignsRef.addListenerForSingleValueEvent(valueEventListener);

Please note, that in order to create a LatLng object, you need to loop throught the DataSnapshot object to get the latitude and longitute. There is no way you can map the children that exist under the location node to a LatLng object. Beside that, LatLng is not a Firebase supported data type.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • it worked but the value that is gotten from firebase still equal null i am storing all values in a list and when trying to display it in a toast that happens https://drive.google.com/file/d/1Nuf6MEObFUODv_0nVbJrRw7jyLudNwqD/view?usp=sharing – Ahmed El Sayed May 15 '19 at 15:06
  • I cannot understand much from that screenshot. Please provide a more explicit one, to see more code. Are trying to get the Toast outside the `onDataChange`? – Alex Mamo May 15 '19 at 15:10
  • exactly trying to get the Toast outside the onDataChange – Ahmed El Sayed May 15 '19 at 15:20
  • i am saving every result in LatLng list called campaignsLocations and want to test the data already received – Ahmed El Sayed May 15 '19 at 15:24
  • Trying to Toast that message outside the `onDataChange` is problem related to the fact that Firebase APIs are asynchronous. For more informations, you can check **[How to return DataSnapshot value as a result of a method?](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774)** out. – Alex Mamo May 15 '19 at 15:41
  • great thanks, the problem that i am trying to call it from List object when i replaced it with ArrayList it worked – Ahmed El Sayed May 15 '19 at 16:09