在Android上过滤Firebase数据库DataSnapshot子项

时间:2017-07-05 13:37:25

标签: android firebase firebase-realtime-database

我想在Android上查询来自Firebase的数据。 这是我的数据my data

这是我的Android Studio代码:

Firebase myFirebaseRef = new Firebase("https://....../myCoffee/"+phone+"");
            myFirebaseRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot snapshot) {
                    int size= (int) snapshot.getChildrenCount();
                    txtMyCoffee.setText(""+size);
                          }
                @Override
                public void onCancelled(FirebaseError firebaseError) {
                }
            });

问题:孩子的总数是4.如何过滤孩子只计算“coffeeNo”为“Acoffee”的孩子?

3 个答案:

答案 0 :(得分:4)

只需改进您的查询,只获取您要查找的结果:

DatabaseReference databaseRef = FirebaseDatabase.getInstance().getReferenceFromUrl("https://....../myCoffee/"+phone+"");
Query acoffeeQuery = databaseRef.orderByChild("coffeeNo").equalTo("Acoffee");
acoffeeQuery.addValueEventListener(new ValueEventListener() {
    Override
    public void onDataChange(DataSnapshot snapshot) {
        final int size = (int) snapshot.getChildrenCount();
        //size is equal to 2
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {}
});

这样,您不必遍历所有结果,也不必手动实现计数器。

答案 1 :(得分:2)

请使用以下代码:

Firebase rootRef = new Firebase("https://....../myCoffee/" + phone);
rootRef.addValueEventListener(new ValueEventListener() {
    Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        int size = 0;
        for (DataSnapshot ds : dataSnapshot.getChildren()) {
            String coffeeNo = ds.child("coffeeNo").getValue(String.class);
            if(coffeeNo.equals("Acoffee")) {
                size++;
            }
        }
        txtMyCoffee.setText(String.valueOf(size));
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {}
});

修改:另一种方法是使用这样的简单query

Firebase rootRef = new Firebase("https://....../myCoffee/"+phone+"");
Query query = rootRef.orderByChild("coffeeNo").equalTo("Acoffee");
query.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        int size = (int) dataSnapshot.getChildrenCount();
        txtMyCoffee.setText(String.valueOf(size));
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {}
});

两种情况下的txtMyCoffee都会显示:2

答案 2 :(得分:1)

Firebase myFirebaseRef = new Firebase("https://....../myCoffee/"+phone+"");
        myFirebaseRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                int count=0;
                for(DataSnapshot item: dataSnapshot.getChildren())
                {
                         String coffeeNo=item.child("coffeeNo").toString();
                         if(coffeeNo.equals("Acofee")
                              count+=1;
                }
            @Override
            public void onCancelled(FirebaseError firebaseError) {
            }
        });

尝试这样的事情