Firebase搜索功能

时间:2018-12-27 06:56:06

标签: java android firebase firebase-realtime-database

代码

        mRecyclerView = (RecyclerView) rootview.findViewById(R.id.recyclerView);
    //Add the data first
    linearLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(linearLayoutManager);


    mDataabseReference = FirebaseDatabase.getInstance().getReference().child("UserData");


    mSearchButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String mSearchText = mEditText.getText().toString();

            mSearch(mSearchText);
        }
    });
    return rootview;
}

private void mSearch(final String mSearchText) {
    Query searchQuery = mDataabseReference;

    FirebaseRecyclerOptions<SearchHelper> options =
            new FirebaseRecyclerOptions.Builder<SearchHelper>()
                    .setQuery(searchQuery, SearchHelper.class)
                    .build();
    firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<SearchHelper, Search.SearchViewHolder>(options) {

        @Override
        protected void onBindViewHolder(@NonNull final Search.SearchViewHolder holder, int position, @NonNull final SearchHelper model) {


            String uid = getRef(position).getKey();
          mDataabseReference.child(uid).startAt(mSearchText).endAt(mSearchText + "\uf8ff").addValueEventListener(new ValueEventListener() {
              @Override
             public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                 for (DataSnapshot ds : dataSnapshot.getChildren()) {
                      SearchHelper search = dataSnapshot.getValue(SearchHelper.class);
                      mSearchList.add(search);

                  }
              }

              @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 onDataChange(DataSnapshot dataSnapshot) {
                  SearchHelper search = dataSnapshot.getValue(SearchHelper.class);
                  mSearchList.add(search);
              }

              @Override
              public void onCancelled(DatabaseError databaseError) {

              }
          });
        }
        @Override
        public SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return null;
        }


    };
    mRecyclerView.setAdapter(firebaseRecyclerAdapter);
    firebaseRecyclerAdapter.startListening();

}

public static class SearchViewHolder extends RecyclerView.ViewHolder {
        View mView;

        public SearchViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
        }

        public void setName(String title) {
            TextView nameText = (TextView) mView.findViewById(R.id.name);
            nameText.setText(title);
        }
        public void setUserName(String title) {
            TextView nameText = (TextView) mView.findViewById(R.id.username);
            nameText.setText(title);
        }
        public void setImage(String image) {
            CircularImageView mDisplayImage = (CircularImageView) mView.findViewById(R.id.circleImageView);
            Picasso.get().load(image).into(mDisplayImage);
        }

没有结果显示...

我尝试了Childevent和ValueEvent 我已经尝试过for循环了,没有循环了

数据库

edon't image description here

我不知道出了什么问题,我也厌倦了尝试,因为也没有错误,我也不知道我要去哪里。有人可以通过指出错误来帮助我吗?

1 个答案:

答案 0 :(得分:2)

在Firebase中进行搜索始终是两步操作:

  1. 订购数据
  2. 根据该顺序过滤数据

您在代码中缺少第1步:您在哪里都没有注明要订购的商品。

例如,如果您尝试按名称搜索用户日期,则查询将变为:

mDataabseReference.child(uid)
                  .orderByChild("Name")
                  .startAt(mSearchText).endAt(mSearchText + "\uf8ff")
                  .addValueEventListener(new ValueEventListener() {

另一个错误是您正在查看dataSnapshot.getChildren()中的onChildAdded。但是onChildAdded已经为每个子节点触发,因此不需要循环:

 public void onChildAdded(DataSnapshot dataSnapshot, String s) {
     SearchHelper search = dataSnapshot.getValue(SearchHelper.class);
     mSearchList.add(search);
 }