代码
private void fetchResults() {
mDatabaseReference.child("Users").child(id).child("Quiz").child("Results").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot resultSnapshot: dataSnapshot.getChildren()) {
Results result = dataSnapshot.getValue(Results.class);
resultsList.add(result);
mAdapter.notifyDataSetChanged();
}
mAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
数据库在结果后还有一个子节点,这是用户的唯一ID ...显然我不知道哪个用户是如此,我如何跳过该子节点来访问其中的信息
数据库Strucutre - https://ibb.co/mL5KFy
答案 0 :(得分:1)
不是在Result.class中获取结果,而是将其作为String和User
的映射private void fetchResults() {
mDatabaseReference.child("Users").child(id).child("Quiz").child("Results").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot resultSnapshot: dataSnapshot.getChildren()) {
Map<String, User> map = (Map) dataSnapshot.getValue();
if (map != null) {
for (String id: map.entrySet()) {
User user = map.get(id);
//DO whatever you wan here
}
}
}
mAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});