在Firebase中搜索数据

时间:2018-11-26 05:43:16

标签: java android firebase firebase-realtime-database

 DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
                DatabaseReference animalsRef = rootRef.child(rootRef.getKey()).child("Personl");
                ValueEventListener eventListener = new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        Boolean found;
                        String search = editText.getText().toString();
                        for(DataSnapshot ds : dataSnapshot.getChildren()) {
                            String movieName = ds.child("firstname").getValue(String.class);
                            found = movieName.contains(search);
                            Log.d("TAG123", movieName + " / " + found);
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {}
                };
                animalsRef.addListenerForSingleValueEvent(eventListener);

I want to search data using child "fullname" and retrieve data under "Persnl" which contains search result

1 个答案:

答案 0 :(得分:0)

我可以想到的最简单的方法,就像@PradyumanDixit所建议的那样,是删除该额外的Personl子级,以便您可以根据fullname属性查询数据库。因此,要解决此问题,请使用以下查询,而不是使用animalsRef引用:

Query query = rootRef.orderByChild("fullname").equalTo("Ali Ali");
query.addListenerForSingleValueEvent(eventListener);

其余代码将正常工作。