从Firebase的父母那里获取第一个孩子

时间:2018-09-17 08:48:47

标签: android firebase firebase-realtime-database

我正在开发一个Android应用程序。我想要父级“ support”的第一个孩子(第一个UID)并将其存储在String变量中。如何从列表中获取第一个UID的值? 我尝试了一种方法。但这不起作用。

@Override
public void onDataChange(DataSnapshot dataSnapshot1) {
    if (dataSnapshot1.exists()) {
        String futureUID = "";
        for(DataSnapshot futureUIDdatasnapshot:dataSnapshot1.getChildren() ){
            futureUID = futureUIDdatasnapshot.getKey();
            break;
        }
        /*Getting the first UID from the list of UID's in queue in 'future'*/
        futureUID = dataSnapshot1.getChildren().iterator().next().getKey();
        /*Moving a card from 'future' to 'serving'*/
        societyServiceUIDReference.child(FIREBASE_CHILD_SERVING).child(futureUID).setValue(FIREBASE_ACCEPTED);
        /*Removing the UID from 'future' after it is placed in 'serving'*/
        societyServiceUIDReference.child(FIREBASE_CHILD_FUTURE).child(futureUID).removeValue();
    }

}

@Override
public void onCancelled(DatabaseError databaseError) {

}

注意:“ futureUID”是我想要的UID

Firebase structure

2 个答案:

答案 0 :(得分:0)

尝试以下操作:

DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("support");
Query queryUid=ref.orderByKey().limitToFirst(1);
queryUid.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot datas : dataSnapshot.getChildren()) {
        String key=datas.getKey();
            }
        }

 @Override
public void onCancelled(DatabaseError databaseError) {

    }
});

答案 1 :(得分:0)

尝试

[Authorize(Policy = "SuperAdmins")]
public async Task<IActionResult> Profile()
{ ... }
  

.orderByKey()。limitToFirst(n)可以解决问题。它按关键字对查询结果进行排序,仅返回前n个结果;在这种情况下1

DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();

请参阅Work with Lists of Data on Android