使用Firebase UI数据库Android搜索视图

时间:2019-01-06 06:35:14

标签: firebase recycler-adapter searchview

是android的初学者。我一直在尝试为自己的searchview实现firebaseDatabaseRecyclerAdapter.,但是我找不到能够放置在onQueryTextChange方法中的正确逻辑。

     @Override
        protected void onStart() {
            super.onStart();
            firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(Users.class, R.layout.users_view, UsersViewHolder.class,query){
                @Override
                protected void populateViewHolder(UsersViewHolder viewHolder, Users model, int position) {
                    final String nameText = model.getNames();
                    final String statusText = model.getStatus();
                    final String phoneText = model.getPhone();
                    final String uidText = model.getUser_id();
                    final String emailText = model.getEmail();
                    viewHolder.setName(nameText);
                    viewHolder.setStatus(statusText);
                    viewHolder.setItemClickListener(new ItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            Intent intent = new Intent(MainActivity.this, UserDataActivity.class);
                            intent.putExtra("name", nameText);
                            intent.putExtra("email", emailText);
                            intent.putExtra("phone", phoneText);
                            intent.putExtra("user_id", uidText);
                            intent.putExtra("status", statusText);
                            startActivity(intent);
                        }
                    });
                }
            };
            mRecyclerView.setAdapter(firebaseRecyclerAdapter);

        }

        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

       @Override
    public boolean onQueryTextChange(String newText) {
        List<Users> filteredList = filter(postList, newText);
        updateList(filteredList);
        return true;
    }
  private List<Users> filter (List<Users> pi, String query){
        query = query.toLowerCase();
        final List<Users> filteredList = new ArrayList<>();
        for(Users users: pi){
            final String text = users.getNames().toLowerCase();
            if(text.contains(query)){
                filteredList.add(users);
            }
        }
        return filteredList;
    }
    public void updateList(List<Users> newList){
        postList = new ArrayList<>();
        postList.addAll(newList);
        firebaseRecyclerAdapter.notifyDataSetChanged();
    }
}

0 个答案:

没有答案