Firebase retrieving multiple objects in multiple nodes
Firebase retrieving multiple objects in multiple nodes
I am trying to read data from a firebase realTime database in android
(For some reason FireBase sorts the location data and then the VideoEntry but I think it's correct anyway...according to the json generated)
In case you needed this is how I add the nodes:
submitVideoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String locationName = locationInput.getText().toString();
double lat = Double.parseDouble(latInput.getText().toString());
double lon = Double.parseDouble(longInput.getText().toString());
String userid = mFirebaseUser.getEmail().substring(0, mFirebaseUser.getEmail().indexOf("@"));
String cleanUserId = userid.replaceAll("W", "");
updateUserInfo(cleanUserId, mFirebaseUser.getEmail(), "nsaofdhiqo3", locationName, lat, lon);
}
});
}
private void updateUserInfo(String userId, String email, String videoId, String locName, double lat, double lon) {
LocationData location = new LocationData(locName,lat,lon);
VideoEntry videoEntry = new VideoEntry(videoId,location);
UserData user = new UserData(email, videoEntry);
mDatabaseRef.child("users").child(userId).setValue(user);
}
{
"users" : {
"valentinogiuseppe00" : {
"mName" : "valentino.giuseppe00@gmail.com",
"mVideoEntry" : {
"mLocationData" : {
"mLatitude" : 58,
"mLongitude" : 10,
"mName" : "locationName"
},
"mVideoId" : "nsaofdhiqo3"
}
}
}
}
This is what I tried so far:
databaseRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
double lat = 0.0, lon = 0.0;
String locName = null;
String fVideoId = null, videoName = null;
String userName = null;
//TODO Need to re-structure the to add markers
for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
Log.d(TAG, "userSnapShot children #:" + userSnapshot.getChildrenCount());
userName = userSnapshot.child(cleanUserId).getValue(String.class);
for (DataSnapshot videoSnapShot : dataSnapshot.getChildren()) {
Log.d(TAG, "videoSnapShot children #:" + videoSnapShot.getChildrenCount());
fVideoId = videoSnapShot.child("videoId").getValue(String.class);
// videoName = videoSnapShot.child("").getValue(String.class);
for (DataSnapshot locationSnapShot : dataSnapshot.getChildren()) {
Log.d(TAG, "locationSnapShot children #:" + (locationSnapShot.getChildrenCount()));
lat = locationSnapShot.child("mLatitude").getValue(Double.class);
lon = locationSnapShot.child("mLongitude").getValue(Double.class);
}
}
}
}
Please let me know if you need something else from me.
I am missing something little since I can see the map that gets generated and all the value within
Thanks fellow Devs
Yes in a nutshell I have 3 models user,video and location. I need those values. Especially location since I am working with maps. (Can you tell me anything regarding that graphic bug regarding the nodes order)
– zepvalue
2 days ago
1 Answer
1
if you want to get all the values inside mVideoEntry
with the data of mLocationData
mVideoEntry
mLocationData
do this
Create a new class that will be your pojo object this will be called userPojo.class
public class UserPojo {
private long mLatitude;
private long mLongitude;
private String mName;
public long getmLatitude() {
return mLatitude;
}
public void setmLatitude(long mLatitude) {
this.mLatitude = mLatitude;
}
public long getmLongitude() {
return mLongitude;
}
public void setmLongitude(long mLongitude) {
this.mLongitude = mLongitude;
}
public String getmName() {
return mName;
}
public void setmName(String mName) {
this.mName = mName;
}
}
then just in your reference iterate inside mVideoEntry child and get your values
databaseRef.child("users").child("valentinogiuseppe00").child("mVideoEntry").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapShot : dataSnapshot.getChildren()){
UserPojo user= snapShot.getValue(UserPojo.class);
//Getting your values
long longitude = user.getmLongitude();
long latitude = user.getmLatitude();
String name = user.getmName();
Log.e("Data: " , "" + latitude + "" + longitude + "" + name);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
with this you should be able to get all your mLocationData
inside each mVideoEntry
mLocationData
mVideoEntry
I already have 3 models classes. I need to retrieve everything from that database. mName,mVideoentry and mLocationData. That unfortunately works only for one node eheh
– zepvalue
2 days ago
you can then add the values that i posted here to them and reference like i do here, but try doing this class alone and try doing this , it should work, then you can just edit your code, but for understaning purposes i think its cleaner
– Gastón Saillén
2 days ago
I already did for one node. My problem is with multiple :( We just missing something really small
– zepvalue
2 days ago
take a look at this link , this should solve your issue quora.com/…
– Gastón Saillén
2 days ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
do you need to get the values inside mLocationData ?
– Gastón Saillén
2 days ago