Firebase Android - startAt()和endAt()无法正常工作?

时间:2017-07-17 07:35:13

标签: android firebase firebase-realtime-database

如何根据用户名搜索用户?我已经看了很多关于此事的SO帖子,但我仍然无法实现我想要做的事情。我试图应用我在这些帖子中看到的内容,如下所示:

DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference("users");
            usersRef.orderByChild("username")
                    .startAt(queryText)
                    .endAt(queryText+"\uf8ff");

            usersRef.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot snapshot) {
                    searchList = new ArrayList<>();
                    for (DataSnapshot postSnapshot: snapshot.getChildren()) {
                        User user = postSnapshot.getValue(User.class);
                        Log.d("USER: ", "" + user.getUsername());

                        searchList.add(user);
                    }

                    adapter = new UserCardAdapter(getContext(), searchList);
                    recyclerView.setAdapter(adapter);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Log.e("onQueryTextChange: " ,databaseError.getMessage());
                }
            });

但是,仍然会检索所有用户。我已经看到startAt()和endAt()的用法可能适用于其他帖子上的其他帖子,但我无法让它为我工作..

这是用户数据的存储方式: User Data Structure

2 个答案:

答案 0 :(得分:2)

您差不多完成了,但是您应该在已经应用addListenerForSingleValueEventorderBy()startAt()的数据库引用之后添加endAt()

 usersRef.orderByChild("username")
                        .startAt(queryText)
                        .endAt(queryText+"\uf8ff")
                        .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot snapshot) {
                    searchList = new ArrayList<>();
                    for (DataSnapshot postSnapshot: snapshot.getChildren()) {
                        User user = postSnapshot.getValue(User.class);
                        Log.d("USER: ", "" + user.getUsername());

                        searchList.add(user);
                    }

                    adapter = new UserCardAdapter(getContext(), searchList);
                    recyclerView.setAdapter(adapter);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Log.e("onQueryTextChange: " ,databaseError.getMessage());
                }
            });

答案 1 :(得分:1)

根据documentation,您对orderBy()startAt()endAt()的使用是正确的。

但是addListener方法必须直接应用于orderByChild()startAt()endAt()方法链返回的对象,而不是在新的语句中使用DatabaseReference检索... .getReference("users")

如果您使用ChildEventListener

DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference("users");
usersRef.orderByChild("username")
        .startAt(queryText)
        .endAt(queryText+"\uf8ff");
        .addChildEventListener(new ChildEventListener() {
          List<User> searchList = new ArrayList<>();

          @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            User user = dataSnapshot.getValue(User.class);
            Log.d("USER: ", "" + user.getUsername());
            searchList.add(user);
          }

          @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) {}
    });