androidandroid-filterable

No data toast in android while search results do not match


I have implemented filterable. I have checked filter results on onPublished i.e if(filterResults.count==0) and toast over there but it shows the toast every time searchtext matches or not, See below for the reference,

Here is my code on adapter:

@Override
public Filter getFilter() {
    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            String charString = charSequence.toString();
            Log.d("cndbdhdh",charString);

            if (charString.isEmpty()) {

                recentUserArrayListArrayListTemp = recentUserArrayListArrayList;
            } else {

                ArrayList<MostRecentUsers.RecentUserList> filteredList = new ArrayList<>();


                for (MostRecentUsers.RecentUserList androidVersion : recentUserArrayListArrayList) {

                    if (androidVersion.getName().toLowerCase().contains(charString) || androidVersion.getPhone().toLowerCase().contains(charString) ) {

                        filteredList.add(androidVersion);
                    }


                }

                recentUserArrayListArrayListTemp = filteredList;
            }

            FilterResults filterResults = new FilterResults();
            filterResults.values = recentUserArrayListArrayListTemp;
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {



            notifyDataSetChanged();
            if (filterResults.count==0){
                Toast.makeText(activity, "no data", Toast.LENGTH_SHORT).show();


            }else { 
                recentUserArrayListArrayListTemp = (ArrayList<MostRecentUsers.RecentUserList>) filterResults.values;

            }



        }
    };
}

Solution

  • You should also do

      filterResults.count = recentUserArrayListArrayListTemp.size();
    

    in your performFiltering