我可以显示以下数组列表,但是我认为我的for循环应该正确完成,但是不会递减……
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Popular Clinics"); // getReference() is the root
reference.addValueEventListener(new ValueEventListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
list.clear();
// displayList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
int count = (int) snapshot.getChildrenCount()-1;
for (int i = count; i >= 0; i--) {
list.add(String.valueOf(snapshot.child(String.valueOf(i)).getValue()));
// displayList.add(snapshot.getValue());
}
答案 0 :(得分:1)
我猜它没有算在内,因为:
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
int count = (int) snapshot.getChildrenCount()-1;
....
}
您正在循环浏览快照中的孩子,这些孩子本身没有孩子,这意味着:
int count = (int) snapshot.getChildrenCount()-1;
返回-1
因此,请以此替换循环并尝试:
for (DataSnapshot snapshot : dataSnapshot) {
list.add(snapshot.getValue());
}