У меня есть RecyclerView, который заполняется из моей базы данных Firebase. Список довольно длинный, поэтому я хочу, чтобы пользователь мог искать в списке. Но когда пользователь вводит первый символ в SearchView, ничего не возвращается.
Это onCreateView в моем любимом фрагменте ОБНОВЛЕНО
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_favourites, container, false);
mAuth = FirebaseAuth.getInstance();
current_user_id = mAuth.getCurrentUser().getUid();
recyclerView = (RecyclerView) view.findViewById(R.id.route_favourites);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
search = (SearchView) view.findViewById(R.id.search);
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("BusRoute");
rootRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot datasnapshot : dataSnapshot.getChildren()){
String key = datasnapshot.getKey();
Query query = rootRef.child(key).orderByChild("route").equalTo(newText);
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
favouritesAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return true;
}
});
favouritesAdapter = new FavouritesAdapter(getDataSetHistory(), getActivity());
favouritesAdapter.notifyDataSetChanged();
recyclerView.setAdapter(favouritesAdapter);
getBusRoutes();
return view;
}
Это мой любимый Адаптер:
public class FavouritesAdapter extends RecyclerView.Adapter<FavouritesAdapter.ViewHolder> implements Filterable {
private List<BusRoute> favourites;
private List<BusRoute> searchList;
private Context context;
public static class ViewHolder extends RecyclerView.ViewHolder{
TextView routeTextView, originTextView, destinationTextView;
ImageView addFavouritesButton;
ViewHolder(View v){
super(v);
this.routeTextView = v.findViewById(R.id.route);
this.destinationTextView = v.findViewById(R.id.destination);
this.originTextView = v.findViewById(R.id.origin);
this.addFavouritesButton = v.findViewById(R.id.addToFavourites);
addFavouritesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String route = routeTextView.getText().toString();
System.out.println("clicked");
System.out.println(route);
//addToFavourites(route);
}
});
}
@NonNull
@Override
public FavouritesAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.favourite_card, parent, false);
return new FavouritesAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull FavouritesAdapter.ViewHolder viewHolder, int i) {
BusRoute bus = favourites.get(i);
viewHolder.originTextView.setText(String.valueOf(bus.getOrigin()));
viewHolder.destinationTextView.setText(String.valueOf(bus.getDestination()));
viewHolder.routeTextView.setText(String.valueOf(bus.getRoute()));
}
public FavouritesAdapter(ArrayList<BusRoute> favourites, Context context){
this.context = context;
this.favourites = favourites;
searchList = new ArrayList<>(favourites);
}
@Override
public int getItemCount() {
System.out.println(favourites);
return favourites.size();
}
@Override
public Filter getFilter() {
return favouriteFilter;
}
private Filter favouriteFilter = new Filter(){
@Override
protected FilterResults performFiltering(CharSequence constraint) {
System.out.println("filter results");
List<BusRoute> filteredList = new ArrayList<>();
if(constraint==null || constraint.length()==0){
filteredList.addAll(searchList);
}else{
String filterPattern = constraint.toString().toLowerCase().trim();
for(BusRoute br : searchList){
if(br.getRoute().toLowerCase().contains(filterPattern)){
filteredList.add(br);
}else{
System.out.println("nothing here");
}
}
}
System.out.println("set filter results");
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
favourites.clear();
favourites.addAll((List)results.values);
notifyDataSetChanged();
}
};
}
Я просто хочу иметь фильтруемую строку поиска, но не могу заставить ее работать! Буду признателен за любую помощь!