Я новичок в 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();
}
}