我試圖將userid存儲到uidMatches中。當我做第一個println時,我得到了正確的匹配字符串數組,但是當我做第二個時,它說我的uidMatches數組中沒有任何內容......「[]」。我不明白這是可能的。將firebase值存儲到數組中?保持爲零
private void populateListView() {
final ArrayList<String> uidMatches = new ArrayList<String>();
final ArrayList<Profile> match_profiles = new ArrayList<Profile>();
users = database.getReference("Users");
user = FirebaseAuth.getInstance().getCurrentUser();
matches = database.getReference("Matches").child(user.getUid());
// Returns uid's for all profiles you matched with
matches.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
String match = child.getValue().toString();
uidMatches.add(match.toString());
System.out.println("HERE ARE MATCHES 1: " + uidMatches);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
System.out.println("HERE ARE MATCHES 2: " + uidMatches);
// Loop to get all Profiles that match each "uidMatches" uid
for (int x=0; x<uidMatches.size(); x++)
{
users.child(uidMatches.get(x)).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren())
{
Profile profile = child.getValue(Profile.class);
match_profiles.add(profile);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
Picture of my firebase setup. I Blocked out the users unique ID's generated from FB login
沒有運行你的代碼我想我看到了問題。當代碼首次啓動時,代碼末尾的println會在數組爲空時運行。另一個println在填充結果之後被觸發onDataChange。 –
請檢查我的數據庫,我把整體佈局。我正在嘗試使用uidMatches查找與id相匹配的所有用戶對象,以便我可以取回用戶屬性。我最好將用戶對象存儲在匹配項下,而不僅僅是他們的uid字符串? – Steve