Getting data from Firebase Database from multiple paths












1















I have a problem with getting data from Firebas eDatabase from multiple paths. I am writing an Endomondo clone and I have a database structure as follows:



users:
user_1:
name:
....
friends:
user_2
user_3
user_2:
...
workouts:
user_1:
workout_1:
distance:
duration:
path:
etc.
user_2:


Now I want to list all workouts of my friends in feed tab. So to do it I need to do three things.



1) Iterate over my profile (user_1) to get all my friends ids.



2) When I have those ids, I need to iterate over their profiles to get their names to display.



3) Iterate over workouts to get all workouts of my friends.

And now I want to put all this information in one object, lets say "FriendWorkout" which would look like:



FriendWorkout:
String friendName;
Workout workout; // contains all workout data(path, time, distance)


And put all of those object in the list so I can pass it to the adapter.



JSON:



{
"users" : {
"BLhXU5eJGtZayRrUGYTrGiImg313" : {
"avatarUrl" : "https://firebasestorage.googleapis.com/v0/b/alphaui-40ee2.appspot.com/o/avatars%2Favatars%2FBLhXU5eJGtZayRrUGYTrGiImg313.jpg?alt=media&token=c6b00937-6970-4f43-8ea4-b9add33c43e1",
"email" : "xhh@gmail.com",
"firstName" : "Jadwiga",
"friends" : {
"CTS134a7vtMWcBs7N2ynDKSyKj23" : true,
"DoXUtoa7vtMWcBs7N2ynDKSyKj23" : true
},
"lastWorkout" : "-LRGN8H6a1esdUPvb-ve",
"surname" : "Szalony"
},
"CTS134a7vtMWcBs7N2ynDKSyKj23" : {
"avatarUrl" : "none",
"email" : "ofca@wp.pl",
"firstName" : "Leokadia",
"friends" : {
"BLhXU5eJGtZayRrUGYTrGiImg313" : true
},
"lastWorkout" : "dasdsa",
"surname" : "Fin"
}
},
"workouts" : {
"BLhXU5eJGtZayRrUGYTrGiImg313" : {
"-LRGN3sEJgrNxRH3Hf6j" : {
"date" : "Wed, 14 Nov 2018 09:39:41",
"distance" : 0,
"duration" : "0:00",
"workoutName" : "Running"
},
"-LRGN68Z_s5dUjlkuhB-" : {
"date" : "Wed, 14 Nov 2018 09:39:50",
"distance" : 0,
"duration" : "0:00",
"workoutName" : "Running"
}
},
"DoXUtoa7vtMWcBs7N2ynDKSyKj23" : {
"-LRGSafxbmKWGLy5x-Kd" : {
"date" : "Wed, 14 Nov 2018 10:03:50",
"distance" : 0,
"duration" : "0:02",
"workoutName" : "Running"
}
}
}
}









share|improve this question

























  • It might be very easy. Please show us your real database structure as a JSON file or at least as a screenshot.

    – Alex Mamo
    Nov 15 '18 at 10:21











  • @AlexMamo updated post with JSON.

    – northenofca
    Nov 15 '18 at 10:31











  • Ok, I'll write you an answer right now.

    – Alex Mamo
    Nov 15 '18 at 10:35
















1















I have a problem with getting data from Firebas eDatabase from multiple paths. I am writing an Endomondo clone and I have a database structure as follows:



users:
user_1:
name:
....
friends:
user_2
user_3
user_2:
...
workouts:
user_1:
workout_1:
distance:
duration:
path:
etc.
user_2:


Now I want to list all workouts of my friends in feed tab. So to do it I need to do three things.



1) Iterate over my profile (user_1) to get all my friends ids.



2) When I have those ids, I need to iterate over their profiles to get their names to display.



3) Iterate over workouts to get all workouts of my friends.

And now I want to put all this information in one object, lets say "FriendWorkout" which would look like:



FriendWorkout:
String friendName;
Workout workout; // contains all workout data(path, time, distance)


And put all of those object in the list so I can pass it to the adapter.



JSON:



{
"users" : {
"BLhXU5eJGtZayRrUGYTrGiImg313" : {
"avatarUrl" : "https://firebasestorage.googleapis.com/v0/b/alphaui-40ee2.appspot.com/o/avatars%2Favatars%2FBLhXU5eJGtZayRrUGYTrGiImg313.jpg?alt=media&token=c6b00937-6970-4f43-8ea4-b9add33c43e1",
"email" : "xhh@gmail.com",
"firstName" : "Jadwiga",
"friends" : {
"CTS134a7vtMWcBs7N2ynDKSyKj23" : true,
"DoXUtoa7vtMWcBs7N2ynDKSyKj23" : true
},
"lastWorkout" : "-LRGN8H6a1esdUPvb-ve",
"surname" : "Szalony"
},
"CTS134a7vtMWcBs7N2ynDKSyKj23" : {
"avatarUrl" : "none",
"email" : "ofca@wp.pl",
"firstName" : "Leokadia",
"friends" : {
"BLhXU5eJGtZayRrUGYTrGiImg313" : true
},
"lastWorkout" : "dasdsa",
"surname" : "Fin"
}
},
"workouts" : {
"BLhXU5eJGtZayRrUGYTrGiImg313" : {
"-LRGN3sEJgrNxRH3Hf6j" : {
"date" : "Wed, 14 Nov 2018 09:39:41",
"distance" : 0,
"duration" : "0:00",
"workoutName" : "Running"
},
"-LRGN68Z_s5dUjlkuhB-" : {
"date" : "Wed, 14 Nov 2018 09:39:50",
"distance" : 0,
"duration" : "0:00",
"workoutName" : "Running"
}
},
"DoXUtoa7vtMWcBs7N2ynDKSyKj23" : {
"-LRGSafxbmKWGLy5x-Kd" : {
"date" : "Wed, 14 Nov 2018 10:03:50",
"distance" : 0,
"duration" : "0:02",
"workoutName" : "Running"
}
}
}
}









share|improve this question

























  • It might be very easy. Please show us your real database structure as a JSON file or at least as a screenshot.

    – Alex Mamo
    Nov 15 '18 at 10:21











  • @AlexMamo updated post with JSON.

    – northenofca
    Nov 15 '18 at 10:31











  • Ok, I'll write you an answer right now.

    – Alex Mamo
    Nov 15 '18 at 10:35














1












1








1








I have a problem with getting data from Firebas eDatabase from multiple paths. I am writing an Endomondo clone and I have a database structure as follows:



users:
user_1:
name:
....
friends:
user_2
user_3
user_2:
...
workouts:
user_1:
workout_1:
distance:
duration:
path:
etc.
user_2:


Now I want to list all workouts of my friends in feed tab. So to do it I need to do three things.



1) Iterate over my profile (user_1) to get all my friends ids.



2) When I have those ids, I need to iterate over their profiles to get their names to display.



3) Iterate over workouts to get all workouts of my friends.

And now I want to put all this information in one object, lets say "FriendWorkout" which would look like:



FriendWorkout:
String friendName;
Workout workout; // contains all workout data(path, time, distance)


And put all of those object in the list so I can pass it to the adapter.



JSON:



{
"users" : {
"BLhXU5eJGtZayRrUGYTrGiImg313" : {
"avatarUrl" : "https://firebasestorage.googleapis.com/v0/b/alphaui-40ee2.appspot.com/o/avatars%2Favatars%2FBLhXU5eJGtZayRrUGYTrGiImg313.jpg?alt=media&token=c6b00937-6970-4f43-8ea4-b9add33c43e1",
"email" : "xhh@gmail.com",
"firstName" : "Jadwiga",
"friends" : {
"CTS134a7vtMWcBs7N2ynDKSyKj23" : true,
"DoXUtoa7vtMWcBs7N2ynDKSyKj23" : true
},
"lastWorkout" : "-LRGN8H6a1esdUPvb-ve",
"surname" : "Szalony"
},
"CTS134a7vtMWcBs7N2ynDKSyKj23" : {
"avatarUrl" : "none",
"email" : "ofca@wp.pl",
"firstName" : "Leokadia",
"friends" : {
"BLhXU5eJGtZayRrUGYTrGiImg313" : true
},
"lastWorkout" : "dasdsa",
"surname" : "Fin"
}
},
"workouts" : {
"BLhXU5eJGtZayRrUGYTrGiImg313" : {
"-LRGN3sEJgrNxRH3Hf6j" : {
"date" : "Wed, 14 Nov 2018 09:39:41",
"distance" : 0,
"duration" : "0:00",
"workoutName" : "Running"
},
"-LRGN68Z_s5dUjlkuhB-" : {
"date" : "Wed, 14 Nov 2018 09:39:50",
"distance" : 0,
"duration" : "0:00",
"workoutName" : "Running"
}
},
"DoXUtoa7vtMWcBs7N2ynDKSyKj23" : {
"-LRGSafxbmKWGLy5x-Kd" : {
"date" : "Wed, 14 Nov 2018 10:03:50",
"distance" : 0,
"duration" : "0:02",
"workoutName" : "Running"
}
}
}
}









share|improve this question
















I have a problem with getting data from Firebas eDatabase from multiple paths. I am writing an Endomondo clone and I have a database structure as follows:



users:
user_1:
name:
....
friends:
user_2
user_3
user_2:
...
workouts:
user_1:
workout_1:
distance:
duration:
path:
etc.
user_2:


Now I want to list all workouts of my friends in feed tab. So to do it I need to do three things.



1) Iterate over my profile (user_1) to get all my friends ids.



2) When I have those ids, I need to iterate over their profiles to get their names to display.



3) Iterate over workouts to get all workouts of my friends.

And now I want to put all this information in one object, lets say "FriendWorkout" which would look like:



FriendWorkout:
String friendName;
Workout workout; // contains all workout data(path, time, distance)


And put all of those object in the list so I can pass it to the adapter.



JSON:



{
"users" : {
"BLhXU5eJGtZayRrUGYTrGiImg313" : {
"avatarUrl" : "https://firebasestorage.googleapis.com/v0/b/alphaui-40ee2.appspot.com/o/avatars%2Favatars%2FBLhXU5eJGtZayRrUGYTrGiImg313.jpg?alt=media&token=c6b00937-6970-4f43-8ea4-b9add33c43e1",
"email" : "xhh@gmail.com",
"firstName" : "Jadwiga",
"friends" : {
"CTS134a7vtMWcBs7N2ynDKSyKj23" : true,
"DoXUtoa7vtMWcBs7N2ynDKSyKj23" : true
},
"lastWorkout" : "-LRGN8H6a1esdUPvb-ve",
"surname" : "Szalony"
},
"CTS134a7vtMWcBs7N2ynDKSyKj23" : {
"avatarUrl" : "none",
"email" : "ofca@wp.pl",
"firstName" : "Leokadia",
"friends" : {
"BLhXU5eJGtZayRrUGYTrGiImg313" : true
},
"lastWorkout" : "dasdsa",
"surname" : "Fin"
}
},
"workouts" : {
"BLhXU5eJGtZayRrUGYTrGiImg313" : {
"-LRGN3sEJgrNxRH3Hf6j" : {
"date" : "Wed, 14 Nov 2018 09:39:41",
"distance" : 0,
"duration" : "0:00",
"workoutName" : "Running"
},
"-LRGN68Z_s5dUjlkuhB-" : {
"date" : "Wed, 14 Nov 2018 09:39:50",
"distance" : 0,
"duration" : "0:00",
"workoutName" : "Running"
}
},
"DoXUtoa7vtMWcBs7N2ynDKSyKj23" : {
"-LRGSafxbmKWGLy5x-Kd" : {
"date" : "Wed, 14 Nov 2018 10:03:50",
"distance" : 0,
"duration" : "0:02",
"workoutName" : "Running"
}
}
}
}






java android firebase firebase-realtime-database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 11:05









Alex Mamo

45.1k82863




45.1k82863










asked Nov 15 '18 at 10:18









northenofcanorthenofca

135




135













  • It might be very easy. Please show us your real database structure as a JSON file or at least as a screenshot.

    – Alex Mamo
    Nov 15 '18 at 10:21











  • @AlexMamo updated post with JSON.

    – northenofca
    Nov 15 '18 at 10:31











  • Ok, I'll write you an answer right now.

    – Alex Mamo
    Nov 15 '18 at 10:35



















  • It might be very easy. Please show us your real database structure as a JSON file or at least as a screenshot.

    – Alex Mamo
    Nov 15 '18 at 10:21











  • @AlexMamo updated post with JSON.

    – northenofca
    Nov 15 '18 at 10:31











  • Ok, I'll write you an answer right now.

    – Alex Mamo
    Nov 15 '18 at 10:35

















It might be very easy. Please show us your real database structure as a JSON file or at least as a screenshot.

– Alex Mamo
Nov 15 '18 at 10:21





It might be very easy. Please show us your real database structure as a JSON file or at least as a screenshot.

– Alex Mamo
Nov 15 '18 at 10:21













@AlexMamo updated post with JSON.

– northenofca
Nov 15 '18 at 10:31





@AlexMamo updated post with JSON.

– northenofca
Nov 15 '18 at 10:31













Ok, I'll write you an answer right now.

– Alex Mamo
Nov 15 '18 at 10:35





Ok, I'll write you an answer right now.

– Alex Mamo
Nov 15 '18 at 10:35












1 Answer
1






active

oldest

votes


















0














What you need can be solved by querying the database three times, once to get the friendUid, second to get the corresponding names and third to get the Workout objects of the corresponding friends. So please use the following lines of code:



String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference friendsRef = rootRef.child("users").child(uid).child("friends");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String friendUid = ds.getKey();

DatabaseReference friendUidRef = rootRef.child("users").child(friendUid);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String firstName = ds.child("firstName").getValue(String.class);
String surname = ds.child("surname").getValue(String.class);
String friendName = firstName + " " + surname;
Log.d(TAG, friendName);

DatabaseReference friendUidWorkoutsRef = rootRef.child("workouts").child(friendUid);
ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<FriendWorkout > workouts = new ArrayList<>(); //List added
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Workout workout = ds.getValue(Workout.class);
Log.d(TAG, workout.getWorkoutName());

//Create an object of FriendWorkout as needed
FriendWorkout friendWorkout = new FriendWorkout(friendName, workout);
workouts.add(friendWorkout); //Add object to the list
}
ListView listView = (ListView) findViewById(R.id.list_view);
ArrayAdapter<Data> arrayAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, list);
listView.setAdapter(arrayAdapter); //Set the adapter
}

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

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

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





share|improve this answer


























  • Thank you so much! I have another question. So let's say I want to add all those new FriendWorkout objects in the list List<FriendWorkout > workouts = new ArrayList<>(); and then notify the adapter that he has new data to load. How to do it in the nice way to avoid unnecessary calls on the adapter?

    – northenofca
    Nov 15 '18 at 11:46











  • In general, other questions that derive from the initial question, should be considered new questions and should pe added here on stackoverflow separately. Seeing that you are a new contribuitor, I just have updated my answer with your new request. Please see my updated answer.

    – Alex Mamo
    Nov 15 '18 at 11:58











  • If you think that my answer and completions helped you, please consider accepting it by clicking the checkmark (✔️) on the left side under the vote arrows. Should change the color in green. I'd appreciate it. Thanks!

    – Alex Mamo
    Nov 15 '18 at 11:59











  • Your answers helped me very much. Thank you. I will keep all your tips/comments in my mind for the future! Thank you, Mr. Alex.

    – northenofca
    Nov 15 '18 at 18:04











  • @northenofca You're welcome, cheers!

    – Alex Mamo
    Nov 15 '18 at 18:06











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317154%2fgetting-data-from-firebase-database-from-multiple-paths%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














What you need can be solved by querying the database three times, once to get the friendUid, second to get the corresponding names and third to get the Workout objects of the corresponding friends. So please use the following lines of code:



String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference friendsRef = rootRef.child("users").child(uid).child("friends");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String friendUid = ds.getKey();

DatabaseReference friendUidRef = rootRef.child("users").child(friendUid);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String firstName = ds.child("firstName").getValue(String.class);
String surname = ds.child("surname").getValue(String.class);
String friendName = firstName + " " + surname;
Log.d(TAG, friendName);

DatabaseReference friendUidWorkoutsRef = rootRef.child("workouts").child(friendUid);
ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<FriendWorkout > workouts = new ArrayList<>(); //List added
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Workout workout = ds.getValue(Workout.class);
Log.d(TAG, workout.getWorkoutName());

//Create an object of FriendWorkout as needed
FriendWorkout friendWorkout = new FriendWorkout(friendName, workout);
workouts.add(friendWorkout); //Add object to the list
}
ListView listView = (ListView) findViewById(R.id.list_view);
ArrayAdapter<Data> arrayAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, list);
listView.setAdapter(arrayAdapter); //Set the adapter
}

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

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

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





share|improve this answer


























  • Thank you so much! I have another question. So let's say I want to add all those new FriendWorkout objects in the list List<FriendWorkout > workouts = new ArrayList<>(); and then notify the adapter that he has new data to load. How to do it in the nice way to avoid unnecessary calls on the adapter?

    – northenofca
    Nov 15 '18 at 11:46











  • In general, other questions that derive from the initial question, should be considered new questions and should pe added here on stackoverflow separately. Seeing that you are a new contribuitor, I just have updated my answer with your new request. Please see my updated answer.

    – Alex Mamo
    Nov 15 '18 at 11:58











  • If you think that my answer and completions helped you, please consider accepting it by clicking the checkmark (✔️) on the left side under the vote arrows. Should change the color in green. I'd appreciate it. Thanks!

    – Alex Mamo
    Nov 15 '18 at 11:59











  • Your answers helped me very much. Thank you. I will keep all your tips/comments in my mind for the future! Thank you, Mr. Alex.

    – northenofca
    Nov 15 '18 at 18:04











  • @northenofca You're welcome, cheers!

    – Alex Mamo
    Nov 15 '18 at 18:06
















0














What you need can be solved by querying the database three times, once to get the friendUid, second to get the corresponding names and third to get the Workout objects of the corresponding friends. So please use the following lines of code:



String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference friendsRef = rootRef.child("users").child(uid).child("friends");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String friendUid = ds.getKey();

DatabaseReference friendUidRef = rootRef.child("users").child(friendUid);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String firstName = ds.child("firstName").getValue(String.class);
String surname = ds.child("surname").getValue(String.class);
String friendName = firstName + " " + surname;
Log.d(TAG, friendName);

DatabaseReference friendUidWorkoutsRef = rootRef.child("workouts").child(friendUid);
ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<FriendWorkout > workouts = new ArrayList<>(); //List added
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Workout workout = ds.getValue(Workout.class);
Log.d(TAG, workout.getWorkoutName());

//Create an object of FriendWorkout as needed
FriendWorkout friendWorkout = new FriendWorkout(friendName, workout);
workouts.add(friendWorkout); //Add object to the list
}
ListView listView = (ListView) findViewById(R.id.list_view);
ArrayAdapter<Data> arrayAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, list);
listView.setAdapter(arrayAdapter); //Set the adapter
}

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

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

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





share|improve this answer


























  • Thank you so much! I have another question. So let's say I want to add all those new FriendWorkout objects in the list List<FriendWorkout > workouts = new ArrayList<>(); and then notify the adapter that he has new data to load. How to do it in the nice way to avoid unnecessary calls on the adapter?

    – northenofca
    Nov 15 '18 at 11:46











  • In general, other questions that derive from the initial question, should be considered new questions and should pe added here on stackoverflow separately. Seeing that you are a new contribuitor, I just have updated my answer with your new request. Please see my updated answer.

    – Alex Mamo
    Nov 15 '18 at 11:58











  • If you think that my answer and completions helped you, please consider accepting it by clicking the checkmark (✔️) on the left side under the vote arrows. Should change the color in green. I'd appreciate it. Thanks!

    – Alex Mamo
    Nov 15 '18 at 11:59











  • Your answers helped me very much. Thank you. I will keep all your tips/comments in my mind for the future! Thank you, Mr. Alex.

    – northenofca
    Nov 15 '18 at 18:04











  • @northenofca You're welcome, cheers!

    – Alex Mamo
    Nov 15 '18 at 18:06














0












0








0







What you need can be solved by querying the database three times, once to get the friendUid, second to get the corresponding names and third to get the Workout objects of the corresponding friends. So please use the following lines of code:



String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference friendsRef = rootRef.child("users").child(uid).child("friends");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String friendUid = ds.getKey();

DatabaseReference friendUidRef = rootRef.child("users").child(friendUid);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String firstName = ds.child("firstName").getValue(String.class);
String surname = ds.child("surname").getValue(String.class);
String friendName = firstName + " " + surname;
Log.d(TAG, friendName);

DatabaseReference friendUidWorkoutsRef = rootRef.child("workouts").child(friendUid);
ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<FriendWorkout > workouts = new ArrayList<>(); //List added
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Workout workout = ds.getValue(Workout.class);
Log.d(TAG, workout.getWorkoutName());

//Create an object of FriendWorkout as needed
FriendWorkout friendWorkout = new FriendWorkout(friendName, workout);
workouts.add(friendWorkout); //Add object to the list
}
ListView listView = (ListView) findViewById(R.id.list_view);
ArrayAdapter<Data> arrayAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, list);
listView.setAdapter(arrayAdapter); //Set the adapter
}

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

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

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





share|improve this answer















What you need can be solved by querying the database three times, once to get the friendUid, second to get the corresponding names and third to get the Workout objects of the corresponding friends. So please use the following lines of code:



String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference friendsRef = rootRef.child("users").child(uid).child("friends");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String friendUid = ds.getKey();

DatabaseReference friendUidRef = rootRef.child("users").child(friendUid);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String firstName = ds.child("firstName").getValue(String.class);
String surname = ds.child("surname").getValue(String.class);
String friendName = firstName + " " + surname;
Log.d(TAG, friendName);

DatabaseReference friendUidWorkoutsRef = rootRef.child("workouts").child(friendUid);
ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<FriendWorkout > workouts = new ArrayList<>(); //List added
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Workout workout = ds.getValue(Workout.class);
Log.d(TAG, workout.getWorkoutName());

//Create an object of FriendWorkout as needed
FriendWorkout friendWorkout = new FriendWorkout(friendName, workout);
workouts.add(friendWorkout); //Add object to the list
}
ListView listView = (ListView) findViewById(R.id.list_view);
ArrayAdapter<Data> arrayAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, list);
listView.setAdapter(arrayAdapter); //Set the adapter
}

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

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

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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 11:52

























answered Nov 15 '18 at 11:04









Alex MamoAlex Mamo

45.1k82863




45.1k82863













  • Thank you so much! I have another question. So let's say I want to add all those new FriendWorkout objects in the list List<FriendWorkout > workouts = new ArrayList<>(); and then notify the adapter that he has new data to load. How to do it in the nice way to avoid unnecessary calls on the adapter?

    – northenofca
    Nov 15 '18 at 11:46











  • In general, other questions that derive from the initial question, should be considered new questions and should pe added here on stackoverflow separately. Seeing that you are a new contribuitor, I just have updated my answer with your new request. Please see my updated answer.

    – Alex Mamo
    Nov 15 '18 at 11:58











  • If you think that my answer and completions helped you, please consider accepting it by clicking the checkmark (✔️) on the left side under the vote arrows. Should change the color in green. I'd appreciate it. Thanks!

    – Alex Mamo
    Nov 15 '18 at 11:59











  • Your answers helped me very much. Thank you. I will keep all your tips/comments in my mind for the future! Thank you, Mr. Alex.

    – northenofca
    Nov 15 '18 at 18:04











  • @northenofca You're welcome, cheers!

    – Alex Mamo
    Nov 15 '18 at 18:06



















  • Thank you so much! I have another question. So let's say I want to add all those new FriendWorkout objects in the list List<FriendWorkout > workouts = new ArrayList<>(); and then notify the adapter that he has new data to load. How to do it in the nice way to avoid unnecessary calls on the adapter?

    – northenofca
    Nov 15 '18 at 11:46











  • In general, other questions that derive from the initial question, should be considered new questions and should pe added here on stackoverflow separately. Seeing that you are a new contribuitor, I just have updated my answer with your new request. Please see my updated answer.

    – Alex Mamo
    Nov 15 '18 at 11:58











  • If you think that my answer and completions helped you, please consider accepting it by clicking the checkmark (✔️) on the left side under the vote arrows. Should change the color in green. I'd appreciate it. Thanks!

    – Alex Mamo
    Nov 15 '18 at 11:59











  • Your answers helped me very much. Thank you. I will keep all your tips/comments in my mind for the future! Thank you, Mr. Alex.

    – northenofca
    Nov 15 '18 at 18:04











  • @northenofca You're welcome, cheers!

    – Alex Mamo
    Nov 15 '18 at 18:06

















Thank you so much! I have another question. So let's say I want to add all those new FriendWorkout objects in the list List<FriendWorkout > workouts = new ArrayList<>(); and then notify the adapter that he has new data to load. How to do it in the nice way to avoid unnecessary calls on the adapter?

– northenofca
Nov 15 '18 at 11:46





Thank you so much! I have another question. So let's say I want to add all those new FriendWorkout objects in the list List<FriendWorkout > workouts = new ArrayList<>(); and then notify the adapter that he has new data to load. How to do it in the nice way to avoid unnecessary calls on the adapter?

– northenofca
Nov 15 '18 at 11:46













In general, other questions that derive from the initial question, should be considered new questions and should pe added here on stackoverflow separately. Seeing that you are a new contribuitor, I just have updated my answer with your new request. Please see my updated answer.

– Alex Mamo
Nov 15 '18 at 11:58





In general, other questions that derive from the initial question, should be considered new questions and should pe added here on stackoverflow separately. Seeing that you are a new contribuitor, I just have updated my answer with your new request. Please see my updated answer.

– Alex Mamo
Nov 15 '18 at 11:58













If you think that my answer and completions helped you, please consider accepting it by clicking the checkmark (✔️) on the left side under the vote arrows. Should change the color in green. I'd appreciate it. Thanks!

– Alex Mamo
Nov 15 '18 at 11:59





If you think that my answer and completions helped you, please consider accepting it by clicking the checkmark (✔️) on the left side under the vote arrows. Should change the color in green. I'd appreciate it. Thanks!

– Alex Mamo
Nov 15 '18 at 11:59













Your answers helped me very much. Thank you. I will keep all your tips/comments in my mind for the future! Thank you, Mr. Alex.

– northenofca
Nov 15 '18 at 18:04





Your answers helped me very much. Thank you. I will keep all your tips/comments in my mind for the future! Thank you, Mr. Alex.

– northenofca
Nov 15 '18 at 18:04













@northenofca You're welcome, cheers!

– Alex Mamo
Nov 15 '18 at 18:06





@northenofca You're welcome, cheers!

– Alex Mamo
Nov 15 '18 at 18:06




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317154%2fgetting-data-from-firebase-database-from-multiple-paths%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values