简体   繁体   中英

firebase how to get child of child?

so i am stuck. i want to retrieve child of child from my firebase database.

my database struture looks like this

users{ 
    user1{
        name:
        regno:
        carmodel:
        caryear}
    user2{
        name:
        regno:
        carmodel:
        caryear}
    user3{
        name:
        regno:
        carmodel:
        caryear}}

Regno = car registration number.

when i type in a "carreg no" and press ok. i want the code to search every carreg. if a match is found execute next set of codes.

so far i have tried to store it in a array. but the code seems to fail when i put reg or any variable name that exist.

 public void input(){

    FirebaseDatabase firebase = FirebaseDatabase.getInstance();
    final DatabaseReference table_user = firebase.getReference("User");
    table_user.addListenerForSingleValueEvent(
            new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    //Get map of users in datasnapshot
                    collectPhoneNumbers((Map<String,Object>) dataSnapshot.getValue());
                }

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





private void collectPhoneNumbers(Map<String,Object> users) {

        ArrayList<Long> phoneNumbers = new ArrayList<>();

        //iterate through each user, ignoring their UID
        for (Map.Entry<String, Object> entry : users.entrySet()){

            //Get user map
            Map singleUser = (Map) entry.getValue();
            //Get phone field and append to list
            phoneNumbers.add((Long) singleUser.get("namesd"));
        }

        System.out.println(phoneNumbers.toString());

        mVoiceInputTv.setText("" + phoneNumbers);
    }
}

i got this from another stackflow. before this i tried creating a for look. it was successfull but result failed 80% of the time. (not always correct).

To solve this, you need to query your database using orderByChild() method like this:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
Query regnoQuery = usersRef.orderByChild("regno").equalsTo(regno);

In which regno argument from equalsTo() is the the actual registration number that you are looking for. Then attach a listener to the regnoQuery and use getChildrenCount() method on the dataSnapshot object. If the number of childrens is noOfChildrens > 0 then execute the set of codes you need.

private FirebaseDatabase mdatabase;

private DatabaseReference mdatabaseRef;


    mdatabase = FirebaseDatabase.getInstance();
    mdatabaseRef = mdatabase.getReference();
  mdatabaseRef.child("users").child("users1").orderByChild("regno").equalsTo(regno).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

    for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {


}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM