I'm using a filtering SearchView. Everything works fine but, I want show toast if no result..
how can do? thank you
I've posted my code below, so any help would be great......
I don't put any code there because I don't know how to display "no result(s) found"
CustomFilter.java
public CustomFilter(ArrayList<Player> filterList, MyAdapter adapter)
{
this.adapter=adapter;
this.filterList=filterList;
}
//FILTERING OCURS
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results=new FilterResults();
//CHECK CONSTRAINT VALIDITY
if(constraint != null && constraint.length() > 0)
{
//CHANGE TO UPPER
constraint=constraint.toString().toUpperCase();
//STORE OUR FILTERED PLAYERS
ArrayList<Player> filteredPlayers=new ArrayList<>();
for (int i=0;i<filterList.size();i++)
{
//CHECK
if(filterList.get(i).getName().toUpperCase().contains(constraint))
{
//ADD PLAYER TO FILTERED PLAYERS
filteredPlayers.add(filterList.get(i));
}
}
results.count=filteredPlayers.size();
results.values=filteredPlayers;
}else
{
results.count=filterList.size();
results.values=filterList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
adapter.players= (ArrayList<Player>) results.values;
//REFRESH
adapter.notifyDataSetChanged();
}
}
myadapter.java
@Override
public int getItemCount() {
return players.size();
}
//RETURN FILTER OBJ
@Override
public Filter getFilter() {
if (filter == null) {
filter = new CustomFilter(filterList, this);
}
return filter;
}
}
You can put below line in your code where you do not find any result.
Toast.makeText(context, text, duration).show();
You can obtain context object like below,
Context context = getApplicationContext();
text
is any string/message that you want to show to use like "No result found"
CharSequence text = "No Results found";
duration
you can set as Toast.LENGTH_LONG
First, declare a Context mcontext
variable in your CustomFilter.java
then accept a Context in your constructor like this
Context mcontext;
public CustomFilter(ArrayList<Player> filterList, MyAdapter adapter, Context con)
{
this.adapter=adapter;
this.filterList=filterList;
this.mcontext=con;
}
On your publishResults
method check if result count is 0 or not.
If it is 0 then show a toast like this.
if(results.count==0)
{
Toast.makeText(mcontext, "No results...", Toast.LENGTH_SHORT).show();
}
else
{
adapter.players= (ArrayList<Player>) results.values;
adapter.notifyDataSetChanged();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.