如何只读取 Android 中的第一个 child 值

How to read only first child value in Android

我是 Firebase 的新手,我想知道有没有办法只获取第一个 child 的值并将其插入到 android 中的微调器中。例如:

就像显示的图像一样,我只想检索名称 "slot x" 而不是获取它的所有值。可以这样做吗?

没问题,你可以 order and limit the number of child nodes retrieved 像这样:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference myRef = rootRef.child("path to your parent node");
Query query = myRef.orderByKey().limiToFirst(1);
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
            String key = childSnapshot.getKey(); // "slot 1"
            String status = childSnapshot.child("status").getValue(String.class); // "occupied"
            ...
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException();
    }
});