Solution 1 :
I have checked you code .. problem is you are assigning wrong value to filterResults
also at wrong place (inside loop). Your code should look like below .
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint == null || constraint.length() == 0) {
filterResults.count = countryModelList.size();
filterResults.values = countryModelList;
} else {
List<CountryModel> resultModel = new ArrayList<>();
String searchString = constraint.toString().toLowerCase();
for (CountryModel itemModel:countryModelList) {
if (itemModel.getCountry().toLowerCase().contains(searchString)) {
resultModel.add(itemModel);
}
}
filterResults.count = resultModel.size();
filterResults.values = resultModel;
}
return filterResults;
}
Problem :
on JDK 1.8
tried casting many ways but none working for me.
public class CountryAdapter extends ArrayAdapter<CountryModel> {
private Context context;
private List<CountryModel> countryModelList;
private List<CountryModel> countryModelListFiltered;
public CountryAdapter(Context context, List<CountryModel> countryModelList) {
super(context, R.layout.custom_list_item, countryModelList);
this.context = context;
this.countryModelList = countryModelList;
this.countryModelListFiltered = countryModelList;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
//Instantiates a layout XML file into its corresponding View objects.
@SuppressLint("ViewHolder")
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_list_item, null, true);
TextView tvCountryName = view.findViewById(R.id.tvCountryName);
ImageView countryFlag = view.findViewById(R.id.imageFlag);
tvCountryName.setText(countryModelListFiltered.get(position).getCountry());
//using glide library to set image to image View.
Glide.with(context).load(countryModelListFiltered.get(position).getFlag()).into(countryFlag);
return view;
}
@Override
public int getCount() {
return countryModelListFiltered.size();
}
@Nullable
@Override
public CountryModel getItem(int position) {
return countryModelListFiltered.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
//Filter Logic
@NonNull
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint == null || constraint.length() == 0) {
filterResults.count = countryModelList.size();
filterResults.values = countryModelList;
} else {
List<CountryModel> resultModel = new ArrayList<>();
String searchString = constraint.toString().toLowerCase();
for (CountryModel itemModel:countryModelList) {
if (itemModel.getCountry().toLowerCase().contains(searchString)) {
resultModel.add(itemModel);
}
filterResults.count = resultModel.size();
filterResults.values = itemModel;
}
}
return filterResults;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
Log.d("Cast:", "publishResults: " + results.values.toString());
countryModelListFiltered = ((List<CountryModel>) results.values);
AffectedCountries.countryModelList = (List<CountryModel>) results.values;
notifyDataSetChanged();
}
};
return filter;
}
}
Comments
Comment posted by ADM
Add the whole code of adapter ..
Comment posted by github.com/techieasif/Covid19-Java/blob/master/Covid19Tracker/…
@ADM Stack overflow showing too much code warning , i have github repository please take a look at: