@Override public Filter getFilter() { Filter filter = new Filter() { @Override protected…
- User
@Override public Filter getFilter() { Filter filter = new Filter() { @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults filterResults = new FilterResults(); if (constraint == null || constraint.length() == 0) { // No filtering required, return the original list filterResults.values = destinationModelList; filterResults.count = destinationModelList.size(); } else { String searchStr = constraint.toString().toLowerCase(); List destinationModels = new ArrayList<>(); for (DestinationModel destinationModel : destinationModelList) { String desName = destinationModel.getDesName().toLowerCase(); if (desName.contains(searchStr)) { // Add the destination to the filtered list if it contains the search query as a substring destinationModels.add(destinationModel); } }
filterResults.values = destinationModels; filterResults.count = destinationModels.size(); } return filterResults; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { filteredDestinationModelList = (List<DestinationModel>) results.values; notifyDataSetChanged(); } }; return filter;} please fix this to the logic i want is if ill type ban, banzali and bango will appear, if ill type banz, banzali will appear and bango wont, if ill type banza only banzali, if banzal, banzali will appear, and banzali if type only banzali will appear the same with other datas, the logic you give is the more character related to the data the more it gone
- Assistant
Here's the updated code to implement the filtering logic you described:
@Override public Filter getFilter() { Filter filter = new Filter() { @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults filterResults = new FilterResults(); if (constraint == null || constraint.length() == 0) { // No filtering required, return the original list filterResults.values = destinationModelList; filterResults.count = destinationModelList.size(); } else { String searchStr = constraint.toString().toLowerCase(); List destinationModels = new ArrayList<>(); for (DestinationModel destinationModel : destinationModelList) { String desName = destinationModel.getDesName().toLowerCase(); if (desName.startsWith(searchStr)) { // Add the destination to the filtered list if it starts with the search query destinationModels.add(destinationModel); } }