答案 0 :(得分:2)
final ArrayList<String> emails = new ArrayList<>();
DatabaseReference dbref = FirebaseDatabase.getInstance().getReference().child("users").child(fuser.getUid()).child("friends");
dbref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
GenericTypeIndicator<Map<String, String>> gti = new GenericTypeIndicator<Map<String, String>>() {};
// this contains the friend's list
Map<String,String> friends = dataSnapshot.getValue(gti);
Iterator<String> iterator = friends.keySet().iterator();
while (iterator.hasNext()){
// now for each friend fetch the email
FirebaseDatabase.getInstance().getReference().child("users").child(iterator.next())
.child("email").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
emails.add(dataSnapshot.getValue(String.class));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
为了提高效率(减少服务器调用),请尝试使用朋友列表存储电子邮件
答案 1 :(得分:0)
假设您有ArrayList<String>
名为friends
,其中包含其firebase ID。然后你可以做类似的事情:
ArrayList<String> friendsMails = new ArrayList<>();
final ProgressDialog progressDialog = new ProgressDialog(Activity.this);
progressDialog.setMessage("Getting emails...");
progressDialog.setIndeterminate(true);
progressDialog.show();
DatabaseReference usersDBRef = FirebaseDatabase.getInstance().getReference();
usersDBRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String mail = String.valueOf(dataSnapshot.child("email").getValue(String.class));
friendsMails.add(mail);
if (friendsMails.size() == friends.size()){
progressDialog.dismiss();
//Do whatever you want with the list of mails
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
for(i = 0; i < friends.size(); i++){
usersDBRef.child("users").child(friends.get(i));
}