选择在firebase中具有特定属性值的节点

时间:2016-06-15 12:00:13

标签: android firebase firebase-realtime-database

我有一个数据库,我想从中选择一个特定的节点。 例如,在我的情况下,我想选择用户名为“fatma”的节点。 那我怎么能在android中做到这一点? my database structure

package com.sifast.appsocle.user;

/**
 * Created by Asus on 14/06/2016.
 */
public class User {

    private String username,email,password,dateOfBirth,signUpDate;

    public User(String username, String email, String password,String dateOfBirth,String signUpDate) {
        this.username = username;
        this.email = email;
        this.password = password;
        this.dateOfBirth=dateOfBirth;
        this.signUpDate=signUpDate;

    }
    public User(){

    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(String dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public String getSignUpDate() {
        return signUpDate;
    }

    public void setSignUpDate(String signUpDate) {
        this.signUpDate = signUpDate;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", email='" + email + '\'' +
                ", password='" + password + '\'' +
                ", dateOfBirth='" + dateOfBirth + '\'' +
                ", signUpDate='" + signUpDate + '\'' +
                '}';
    }
}

1 个答案:

答案 0 :(得分:1)

使用以下代码检索指定数据

DatabaseReference users = FirebaseDatabase.getInstance().getReference("users");
    Query query = users.orderByChild("username").equalTo("fatma");
    query.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    User user = dataSnapshot.getValue(User.class));
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
    }

好的,如果您有多个节点,子节点“username”=“fatma”,您应该使用以下代码:

  DatabaseReference users = FirebaseDatabase.getInstance().getReference("users");
            Query query = users.orderByChild("username").equalTo("fatma");
            List<User> users = new ArrayList<>();
            query.addChildEventListener(new ChildEventListener() {
                        @Override
                        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                                     users.add(dataSnapshot.getValue(User.class);
}

                        @Override
                        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                        }

                        @Override
                        public void onChildRemoved(DataSnapshot dataSnapshot) {

                        }

                        @Override
                        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });

这将返回用户列表:)