как говорится в теме.Я хочу иметь возможность искать пользователей по имени пользователя с помощью поиска.Я уже показываю всех пользователей в обзоре переработчиков, но не могу найти способ отфильтровать их.Я использовал облачный firestore.Это вообще возможно?Или мне на самом деле нужна третья часть?Все руководства и учебники, которые я нашел на данный момент, находятся в базе данных Firebase ..
Код адаптера.
public class AllUserAdapter extends FirestoreRecyclerAdapter<AllUsers, AllUserAdapter.AllUsersHolder> {
private OnItemClicklistener mOnItemClicklistener;
public AllUserAdapter(@NonNull FirestoreRecyclerOptions<AllUsers> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull AllUsersHolder holder, int position, @NonNull AllUsers model) {
holder.textViewUsername.setText(String.valueOf(model.getName()));
holder.textViewEmail.setText(model.getEmail());
holder.setAvatar(model.getAvatar());
}
@NonNull
@Override
public AllUsersHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.all_user_list_display, viewGroup, false);
return new AllUsersHolder(view);
}
class AllUsersHolder extends RecyclerView.ViewHolder{
TextView textViewUsername;
TextView textViewEmail;
ImageView imageViewAvatar;
public AllUsersHolder(@NonNull View itemView) {
super(itemView);
textViewUsername = itemView.findViewById(R.id.all_user_username);
textViewEmail = itemView.findViewById(R.id.all_user_userEmail);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION && mOnItemClicklistener!= null){
mOnItemClicklistener.onItemClick(getSnapshots().getSnapshot(position), position);
}
}
});
}
public void setAvatar(String avatar){
imageViewAvatar = (ImageView) itemView.findViewById(R.id.all_user_profile_image);
Picasso.get().load(avatar).into(imageViewAvatar);
}
}
public interface OnItemClicklistener {
void onItemClick(DocumentSnapshot snapshot, int position);
}
public void setOnItemClickListener(OnItemClicklistener listener){
mOnItemClicklistener = listener;
}
}
Список пользователей, показывающий пользователей с recylerview.
public class AllUserListFragment extends Fragment{
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference allUsers = db.collection("users");
private AllUserAdapter mUserAdapter;
public AllUserListFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_all_user_list, container, false);
Query query = allUsers;
FirestoreRecyclerOptions<AllUsers> recyclerOptions = new FirestoreRecyclerOptions.Builder<AllUsers>()
.setQuery(query, AllUsers.class)
.build();
mUserAdapter = new AllUserAdapter(recyclerOptions);
RecyclerView recyclerView = view.findViewById(R.id.allUser_listView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(mUserAdapter);
mUserAdapter.setOnItemClickListener(new AllUserAdapter.OnItemClicklistener() {
@Override
public void onItemClick(DocumentSnapshot snapshot, int position) {
AllUsers allUsers = snapshot.toObject(AllUsers.class);
String id = snapshot.getId();
Bundle bundle = new Bundle();
bundle.putString("visit_user_id", id);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
AllUserProfileFragment fragment = new AllUserProfileFragment();
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.frameLayout, fragment);
fragmentTransaction.commit();
}
});
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
mUserAdapter.startListening();
}
}