The project I'm working on has 3 "tables" in firebase realtime database namely:
Users(contains login information of the users e.g email, passwords mobile numbers etc)
VictimProfiles(Contains basic profile information like profile pic string, name, job title and the corresponding UserID from Users table)
VolunteerProfiles(contains the Address,latitude,longitude,availability timing and the corresponding UserID and VictimProfileID (the ones generated by Firebase))
in my Google Maps activity, I'm required to drop markers on the location of each volunteer as well as the user currently using the app, with a customized info window showing the necessary info on the volunteers, however, as shown in the code below, when I try to access the VictimProfile and Users table from within the ValueEventListener of the volunteer table, their data does not get written to the public strings I'm writing it to. I know it sounds convoluted, but take a look at the code and you'll see what I mean
public void DropVolunteerMarkers()
{
//DROP ALL MARKERS FUNCTION
volunteerDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot volunteerprofile:dataSnapshot.getChildren())
{
key=(String) volunteerprofile.child("UserID").getValue();
victimID=(String) volunteerprofile.child("VictimProfileID").getValue();
getVictimInfo();//GET NAME,PIC,JOB TITLE FROM THE VICTIM TABLE FOR THIS SPECIFIC VOLUNTEER
getUserInfo();//GET EMAIL AND PHONE NUMBER FROM USERS TABLE FOR THIS SPECIFIC VOLUNTEER
String currentuserid=SaveSharedPreference.getUserName(FindNearbyVolunteers.this);
if(!key.equals(currentuserid))//TO AVOID CURRENT USER BEING SHOWN AS A VOLUNTEER
{
address= (String)volunteerprofile.child("Address").getValue();
availabiliity=(String)volunteerprofile.child("Availability").getValue();
String LAT= (String) volunteerprofile.child("Latitude").getValue();
String LNG= (String) volunteerprofile.child("Longitude").getValue();
Toast.makeText(FindNearbyVolunteers.this,LAT+", "+LNG,Toast.LENGTH_SHORT).show();
Double Lat= Double.parseDouble(LAT);
Double Long= Double.parseDouble(LNG);
LatLng latLng = new LatLng(Lat,Long);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
Toast.makeText(FindNearbyVolunteers.this,key+", "+victimID+", "+name+", "+jobtitle+", "+availabiliity+", "+email+", "+address,Toast.LENGTH_LONG).show();
//mMap.setInfoWindowAdapter(new VolunteerPopupAdapter(FindNearbyVolunteers.this,name,jobtitle,availabiliity,email,address,dp,number));
markerOptions.title(volunteerprofile.getKey().toString());
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
mCurrLocationMarker = mMap.addMarker(markerOptions);
}
else{
//IN CASE OF CURRENT USER
//Place current location marker
LatLng latLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("YOU");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
mCurrLocationMarker = mMap.addMarker(markerOptions);
}}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
the getVictimInfo() method:
private void getVictimInfo()
{
victimDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot victimProfile:dataSnapshot.getChildren())
{
if(victimProfile.getKey().equals(victimID))
{
dp=(String)victimProfile.child("DP").getValue();
jobtitle=(String)victimProfile.child("JobTitle").getValue();
name=(String)victimProfile.child("Name").getValue();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
the getUserInfo() method
private void getUserInfo(){
userDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userProfile:dataSnapshot.getChildren())
{
if(userProfile.getKey().equals(key))
{
email=(String)userProfile.child("Email").getValue();
number=(String)userProfile.child("Mobile").getValue();;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
the string where I'm supposed to save the values
DatabaseReference volunteerDatabaseReference,userDatabaseReference,victimDatabaseReference;
String dp,jobtitle="no value",name="no value";
String email="no value",number="no value",address="no value",availabiliity="no value",victimID,key;
my Database screenshot enter image description here