Firebase数据库搜索查询问题

时间:2019-07-25 21:15:28

标签: java android firebase firebase-realtime-database

我正在尝试创建搜索功能,以便用户可以搜索其他已注册的功能。我设法使搜索工作,但没有给出期望的结果。无论我输入什么关键字,它都会在搜索结果中显示所有注册用户,而不仅仅是与该关键字相关的用户。

我不是Java方面的专家,但是我尝试更改和重新排列某些行,但结果仍然相同。

private void SearchPeopleAndFriends(String searchBoxInput)
{
    FirebaseRecyclerOptions<FindFriends> options = new FirebaseRecyclerOptions.Builder<FindFriends>().
            setQuery(UsersRef, FindFriends.class).build(); //query build past the query to FirebaseRecyclerAdapter
    FirebaseRecyclerAdapter<FindFriends, FindFriendsActivity.FindFriendViewHolder> adapter=new FirebaseRecyclerAdapter<FindFriends, FindFriendViewHolder>(options)
    {
        @Override
        protected void onBindViewHolder(@NonNull FindFriendsActivity.FindFriendViewHolder holder, int position, @NonNull FindFriends model)
        {
            holder.username.setText(model.getFullname());
            holder.status.setText(model.getStatus());
            Picasso.get().load(model.getProfileimage()).into(holder.profileimage);

            holder.itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Intent findOthersIntent = new Intent(FindFriendsActivity.this, FindFriendsActivity.class);
                    startActivity(findOthersIntent);

                }
            });
        }
        @NonNull
        @Override
        public FindFriendsActivity.FindFriendViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
        {
            View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.all_user_display_layout,viewGroup,false);

            FindFriendsActivity.FindFriendViewHolder viewHolder=new FindFriendsActivity.FindFriendViewHolder(view);
            return viewHolder;
        }
    };

    SearchResultList.setAdapter(adapter);
    adapter.startListening();
}

public class FindFriendViewHolder extends RecyclerView.ViewHolder
{
    TextView username, status;
    CircleImageView profileimage;

    public FindFriendViewHolder(@NonNull View itemView)
    {
        super(itemView);
        username = itemView.findViewById(R.id.all_user_profile_name);
        status = itemView.findViewById(R.id.all_user_status);
        profileimage = itemView.findViewById(R.id.all_user_profile_pic);
    }
}

与其在搜索结果中带来与关键字相关的用户,不如在数据库中带来所有用户。我的目标是仅显示包含输入关键字的任何用户名

这是我的模型课

public class FindFriends {

    public String profileimage, fullname, status;

    public FindFriends()
    {

    }

    public String getProfileimage() {
        return profileimage;
    }

    public void setProfileimage(String profileimage) {
        this.profileimage = profileimage;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public FindFriends(String profileimage, String fullname, String status) {
        this.profileimage = profileimage;
        this.fullname = fullname;
        this.status = status;
    }
}

我的Firebase参考:

 mAuth = FirebaseAuth.getInstance();
    currentUserID = mAuth.getCurrentUser().getUid();
    UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");

0 个答案:

没有答案