从Android Studio中的Firebase中检索

时间:2017-04-04 15:53:25

标签: android firebase firebase-realtime-database

我的firebase数据库的结构如下所示:

-Details
    -BNFJ3491
        -Answer: "abc"
        -Name: "ABC"
        -Password:"cde"
        -Security Question: "rgrg"
        -User Type: "Student"
    -CBTV15873
        -Answer: "abc"
        -Name: "ABC"
        -Password:"cde"
        -Security Question: "rgrg"
        -User Type: "Student"
-Notices
    -B tech placement
        -Department: "Urja Mandir"
        -Matter: "Hello welcome to btech placement cell"
Firebase ref=new Firebase("https://linkb-873e1.firebaseio.com/Details");
ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    //get all the children

                    for (DataSnapshot child : dataSnapshot.getChildren()) {
                        for (DataSnapshot child1 : child.getChildren()) {


                        }
                    }
                }


                @Override
                public void onCancelled(FirebaseError firebaseError) {
                    System.out.println("The read failed: " + firebaseError.getMessage());
                }

            });

我无法弄清楚如何获取特定ID的密码(比如BNFJ3491)数据快照会获取所有数据但是如何检索有关特定ID的信息。

2 个答案:

答案 0 :(得分:1)

请使用此代码:

Firebase ref=new Firebase("https://linkb-873e1.firebaseio.com/Details");
ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String password = dataSnapshot.child(-BNFJ3491).child("Password").getValue();
            }


            @Override
            public void onCancelled(FirebaseError firebaseError) {
                System.out.println("The read failed: " + firebaseError.getMessage());
            }

        });

答案 1 :(得分:0)

您可以使用此代码

ref.child(userId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    String password = dataSnapshot.child("Password").getValue(String.class);
 }

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