从数据库中的第一项获取特定数据

时间:2018-01-16 08:28:13

标签: java android firebase firebase-realtime-database

我想动态地从Firebase获取特定数据。这是我的结构:

get specific data dynamically

在此图片中获取2“收银员”值如下:

FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
final DatabaseReference databaseReferenceMyCurrentQueue = firebaseDatabase.getReference("Cashier");

databaseReferenceMyCurrentQueue.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String queuedNumber = String.valueOf(dataSnapshot.getValue());
            currentqueue1.setText(queuedNumber);
        }

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

如何在删除包含值为5的myCurrentQueue的收银员交易的第一个孩子后,将实时提取的值myCurrentQueue从5提取到6?提前谢谢。

所以每次删除,实时myCurrentQueue也会改变。

1 个答案:

答案 0 :(得分:1)

FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();


final DatabaseReference databaseReferenceMyCurrentQueue = firebaseDatabase.getReference("CashierTransaction");

databaseReferenceMyCurrentQueue.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
          //get Data as hashMap<String, YourClass>
          //then check the lowest number and update the queue
          firebaseDatabase.getReference("Cashier").setValue(ThatNumber);

        }

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

final DatabaseReference databaseReferenceMyCurrentQueue = firebaseDatabase.getReference("Cashier");

databaseReferenceMyCurrentQueue.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String queuedNumber = String.valueOf(dataSnapshot.getValue());
            currentqueue1.setText(queuedNumber);
        }

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