如果所选值与Firebase数据库值中的值相同,则获取子列表

时间:2017-03-28 22:52:32

标签: android firebase firebase-realtime-database

我的数据库如下:

List_of_users>
             sunil dhakal>
                        departmnet:"Computer Science",
                        level:"Level 3 Undergraduate",
                        name:"sunil dhakal",
                        student_id:"1412361",
                        username:"1412361@my.brunel.ac.uk",
root.child("List_of_user").addValueEventListener(new ValueEventListener() {

    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot data :dataSnapshot.getChildren()){
            if (data.child(department).exists()&& data.child(level).exists()){
                Set<String> set = new HashSet<String>();
                Iterator i = dataSnapshot.child("List_of_users").getChildren().iterator();
                while (i.hasNext()) {
                    set.add(((DataSnapshot) i.next()).getKey());
                }
                list_of_rooms.clear();
                list_of_rooms.addAll(set);
                arrayAdapter.notifyDataSetChanged();
            }
            else{
                Toast.makeText(searchuser.this,"There is not user with the department and level that was selected",Toast.LENGTH_SHORT).show();
                return;
            }
        }
    }

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

我有一个微调器,用户可以在其中选择部门和级别,然后单击一个按钮,该按钮列出数据库中具有该部门和级别的所有用户。

2 个答案:

答案 0 :(得分:0)

使用firebase查询获取它们:

firebaseReference.child("List_of_users")
                 .orderByChild("department")
                 .equalTo("valueInSpinner")
                 .orderByChild("level")
                 .equalTo("valueInSpinner")

然后你可以使用它与适配器将它绑定到ListView。

希望这有帮助。

答案 1 :(得分:0)

searchusers.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v) {
            final String department = dep.getSelectedItem().toString().trim();
            final String level = lev.getSelectedItem().toString().trim();
            root.addValueEventListener(new ValueEventListener() {

                public void onDataChange(DataSnapshot dataSnapshot) {
                    root.child("List_of_users")
                            .orderByChild("department")
                            .equalTo(department)
                            .orderByChild("level")
                            .equalTo(level);
                    Set<String> set = new HashSet<String>();
                    Iterator i = dataSnapshot.child("List_of_users").getChildren().iterator();
                    while (i.hasNext()) {
                        set.add(((DataSnapshot) i.next()).getKey());
                    }
                    list_of_rooms.clear();
                    list_of_rooms.addAll(set);
                    arrayAdapter.notifyDataSetChanged();
                }

这种格式好吗?