Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – How to show Toast when Search is empty in onQuerTextListner

Posted on November 14, 2022

Solution 1 :

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
    list.clear();
    list.addAll((Collection<? extends Allcontainlist>) results.values);
    if (list.size() == 0) {
         //Here you can either use a callback(recommended) or show toast
         //Toast.makeText(mContext, "No Results Matching Your Query...", Toast.LENGTH_SHORT).show();
      }

    notifyDataSetChanged();
}

Problem :

I am using simple recyclerview to show list i able to filter my list but i want to show simple toast when search item is not found in my list please help me for that here is my code

for adapter class

 Filter filter=new Filter() {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        ArrayList<Allcontainlist> filter_list=new ArrayList<>();
        if (constraint==null||constraint.length()==0){
           filter_list.addAll(Alllist);

        }else
        {
            String filter_pattern=constraint.toString().toLowerCase().trim();
            for (Allcontainlist allcontainlist:Alllist){
                if(allcontainlist.getRout().toLowerCase().contains(filter_pattern))
                {
                    filter_list.add(allcontainlist);
                }
            }
        }

        FilterResults results=new FilterResults();
        results.values=filter_list;
        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        list.clear();
        list.addAll((Collection<? extends Allcontainlist>) results.values);
        notifyDataSetChanged();
    }
};

and this code for search lists

  @Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
    inflater.inflate(R.menu.search_bar,menu);
    MenuItem menuItem=menu.findItem(R.id.search_bar);
    SearchView searchView = (SearchView) menuItem.getActionView();
    searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
    searchView.setQueryHint("Search buses");
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            if(list.contains(query)) {
                alladapter.getFilter().filter(query);
            }else
            {
                Toast.makeText(getActivity(),"Not found",Toast.LENGTH_SHORT).show();
            }
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

                if(TextUtils.isEmpty(newText))
                {

                    Toast.makeText(getActivity(),"not found",Toast.LENGTH_SHORT).show();
                }
                else
               alladapter.getFilter().filter(newText);

            return false;
        }
    });
    super.onCreateOptionsMenu(menu, inflater);
}

I have tried some example code but not working please help me for this i want to just toast when the search result is empty thank you

Comments

Comment posted by Hoàng Vũ Anh

creat an callback but when you you search with filter, adapter change everytime when you typing, that’s not good.

READ  [ANSWERED] How can I disable warnings in Android Studio using Flutter
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress