从android中的firebase数据库搜索数据

时间:2017-11-09 17:22:35

标签: android firebase firebase-realtime-database

您好我正在尝试从firebase数据库中搜索数据。但是我没有看到来自firebase的搜索数据的任何选项。在我的firebase数据库中,有地址,城市和手机号码。如果我输入手机号码的最后一位,我想从中搜索数据。我想获得手机号码的结果。

这是我的代码

  floating_search_view_recruiter = (FloatingSearchView) findViewById(R.id.floating_search_view_recruiter);
        floating_search_view_recruiter.setSearchHint("Search Recruiter..");
        floating_search_view_recruiter.attachNavigationDrawerToMenuButton(drawer);
        floating_search_view_recruiter.setOnMenuItemClickListener(new FloatingSearchView.OnMenuItemClickListener() {
            @Override
            public void onActionMenuItemSelected(MenuItem item) {
                int id = item.getItemId();
                if (id == R.id.action_filter) {
                    aleartDialogFilter();
                }
            }
        });

        fab_recyclerView.setVisibility(View.GONE);
        floating_search_view_recruiter.setOnQueryChangeListener(new FloatingSearchView.OnQueryChangeListener() {
            @Override
            public void onSearchTextChanged(String oldQuery, String newQuery) {
                //recruiterAdapter.getFilter().filter(newQuery);
                mRef.orderByChild("city")
                        .startAt(newQuery)
                        .endAt(newQuery + "\uf8ff");

                mRef.child("city").addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }
        });

1 个答案:

答案 0 :(得分:0)

请参阅此github存储库以获取更多示例 enter link description here

mDatabase = FirebaseDatabase.getInstance().getReference().child("appointment");

            Query query = mDatabase.orderByChild("patient").equalTo(restoredText.toString());

            query.addValueEventListener(new ValueEventListener() {

                @Override

                public void onDataChange(DataSnapshot dataSnapshot) {


                    if (dataSnapshot != null || dataSnapshot.getChildrenCount() > 0) {

                        collectPhoneNumbers((Map<String, Object>) dataSnapshot.getValue());

                    }

                }


                @Override

                public void onCancelled(DatabaseError databaseError) {

                    // Failed to read value

                    Toast.makeText(getApplicationContext(), "Failed to read value." + databaseError.toException(), Toast.LENGTH_LONG).show();

                }

            });


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


        if (users != null && !users.isEmpty()) {

            contacts.clear();

            contacts.add(new Appointment("<b>Date</b>", "<b>Time</b>", "<b>Doctor Name</b>", "<b>Patient Name</b>"));

            //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

                String date = (String) singleUser.get("date");

                String time = (String) singleUser.get("time");

                String doctor = (String) singleUser.get("doctor");

                String patient = (String) singleUser.get("patient");

                contacts.add(new Appointment(date, time, doctor, patient));

// Toast.makeText(getApplicationContext(), "Date: " + date + ", Time:" + time + ", Doctor:" + doctor + ", Patient:" + patient, Toast.LENGTH_LONG).show();

                adapter.notifyDataSetChanged();

            }

        }

    }