Android Studio Firebase查询搜索返回对象

时间:2018-06-01 18:49:32

标签: android firebase firebase-realtime-database

我已经将这部分编程了整整一周,并且无法找到解决方案。 Firebase数据库如下所示: Firebase数据库结构

enter image description here

FIREBASE - >学生(数据库) - >

{0}
|- ISIC: 1234567
|- name: "Vladimir"
|- password: "vlad"
|- indeks: "111/11"
|- meals
    |-{0} - amount: 15
    |       name: breakfast
    |-{1} - amount: 2
    |       name: lunch
{1}
|- ISIC: 98765
|- name: "John"
|- password: "doe"
|- indeks: 999/99
|- meals
    |-{0} - amount: 18
    |       name: breakfast
    |-{1} - amount: 20
    |       name: lunch
,...,
{n} (nodes)
|- (Data)
|- (Data)

当我点击Login的按钮,其中Username是indeks并且输入了密码时,我想首先查询数据库查找具有该减压的学生(按用户名)并检查< strong>如果该学生的密码与数据库中的密码完全相同。

我的代码如下:

EditText userName;
EditText pass;
Button logIn;

DatabaseReference dbStudents;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    userName = (EditText) findViewById(R.id.username);
    pass = (EditText) findViewById(R.id.password);
    logIn = (Button) findViewById(R.id.login);

    dbStudents = 

FirebaseDatabase.getInstance()getReference(&#34;学生&#34);

    logIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //some code which I don't know
        }
    });
}

我的麻烦是如何从Firebase检索整个对象Student并在登录成功后使用它。 一直在使用:Query, addListenerForSingleValueEvent and orderBy().getData(),但无法通过输入的字符串找到值,并将其作为对象学生检索。

感谢提前! :)

UPDATE !!

public void getKey() {
        dbStudents = FirebaseDatabase.getInstance().getReference("students");
        dbStudents.orderByChild("indeks").equalTo(userName.getText().toString())
                .addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                            key = snapshot.getKey();
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
    }

有了这个,我可以得到节点(学生)的密钥,其中包含&#34; indeks&#34;值(作为用户名输入),但现在我需要使用Firebase数据库中的密钥(字符串)作为对象学生进行检索。我该怎么办?

更新2 !!

用餐类:

public class Meal {
    private int amount;
    private String name;

    public Meal() {

    }

    public Meal(int amount, String name) {
        this.amount = amount;
        this.name = name;
    }

    public int getAmount() {
        return amount;
    }

    public String getName() {
        return name;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Meal{" +
                "amount=" + amount +
                ", name='" + name + '\'' +
                '}';
    }
}

学生班:

public class Student {
private int ISIC;
private String born;
private String contact;
private String description;
private String imagePath;
private String indeks;
private String issued;
private Meal[] meals;
private String name;
private String password;
private String studies;
private String validity;

    public Student() {

    }

    public Student(int ISIC, String born, String contact, String description, String imagePath,
                   String indeks, String issued, Meal[] meals, String name, String password,
                   String studies, String validity) {
        this.ISIC = ISIC;
        this.born = born;
        this.contact = contact;
        this.description = description;
        this.imagePath = imagePath;
        this.indeks = indeks;
        this.issued = issued;
        this.meals = meals;
        this.name = name;
        this.password = password;
        this.studies = studies;
        this.validity = validity;
    }

    public int getISIC() {
        return ISIC;
    }

    public String getBorn() {
        return born;
    }

    public String getContact() {
        return contact;
    }

    public String getDescription() {
        return description;
    }

    public String getImagePath() {
        return imagePath;
    }

    public String getIndeks() {
        return indeks;
    }

    public String getIssued() {
        return issued;
    }

    public Meal[] getMeals() {
        return meals;
    }

    public String getName() {
        return name;
    }

    public String getPassword() {
        return password;
    }

    public String getStudies() {
        return studies;
    }

    public String getValidity() {
        return validity;
    }


    @Override
    public String toString() {
        return "Student{" +
                "ISIC=" + ISIC +
                ", born='" + born + '\'' +
                ", contact='" + contact + '\'' +
                ", description='" + description + '\'' +
                ", imagePath='" + imagePath + '\'' +
                ", indeks='" + indeks + '\'' +
                ", issued='" + issued + '\'' +
                ", meals=" + meals +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", studies='" + studies + '\'' +
                ", validity='" + validity + '\'' +
                '}';
    }

    public void setISIC(int ISIC) {
        this.ISIC = ISIC;
    }

    public void setBorn(String born) {
        this.born = born;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setImagePath(String imagePath) {
        this.imagePath = imagePath;
    }

    public void setIndeks(String indeks) {
        this.indeks = indeks;
    }

    public void setIssued(String issued) {
        this.issued = issued;
    }

    public void setMeals(Meal[] meals) {
        this.meals = meals;
    }

    public void setName(String name) {
        this.name = name;
    }

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

    public void setStudies(String studies) {
        this.studies = studies;
    }

    public void setValidity(String validity) {
        this.validity = validity;
    }
}

1 个答案:

答案 0 :(得分:0)

您需要执行此方法才能获取每个用户的密码,但我建议将userID保存为每个用户的主节点,而不是保存0,1,2,3,这样您就可以从用户那里获得所需的密码密码,此方法将获取所有用户的所有密码,用mAuth.getCurrentUser().getUid();替换自动生成的0,1,2,3

 logIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String uid = mAuth.getCurrentUser().getUid();
                mRef.child("students").child(uid).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                try {
                    for(DataSnapshot snap :  dataSnapshot.getChildren())
                   YourPojoClass obj = snap.getValue(YourPojoClass.class);
                    String password =obj.getPassword();
                     //here compare your local password inserted in your editText with the one pulled from firebase


    }
                }catch (Exception ex){
                 //error

                }
            }

            @Override
            public void onCancelled(DatabaseError error) {
             //cancelled
            }
        });
    }

请记住,YourPojoClass.class将是一个具有setter和getter的类,具有数据库变量的确切名称

确切地说这是YourPojo.class

public class Student {
    private int ISIC;
    private String indeks;
    private Meal[] meals;
    private String name;
    private String password;
}

请记住为它做固定器和吸气剂!

更新!

所以,现在,既然你得到了这个密钥,就可以访问其中的数据

public void getKey() {
        dbStudents = FirebaseDatabase.getInstance().getReference("students");
        dbStudents.orderByChild("indeks").equalTo(userName.getText().toString())
                .addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                            key = snapshot.getKey();
                            Student std = snapshot.getValue(Student.class);
                            String password = std.getPassword();
                            String name = std.getName(); 
                            //... And so on with the other data


                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
    }