简体   繁体   English

如果没有结果,敬酒

[英]show toast if no result

I'm using a filtering SearchView. 我正在使用过滤式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 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 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" text是您要显示使用的任何字符串/消息,例如“未找到结果”

CharSequence text = "No Results found";

duration you can set as Toast.LENGTH_LONG duration可以设置为Toast.LENGTH_LONG

First, declare a Context mcontext variable in your CustomFilter.java then accept a Context in your constructor like this 首先,在CustomFilter.java声明一个Context mcontext变量,然后像这样在构造函数中接受一个Context

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. 在您的publishResults方法上,检查结果计数是否为0。

If it is 0 then show a toast like this. 如果为0,则显示这样的烤面包。

if(results.count==0)
{
 Toast.makeText(mcontext, "No results...", Toast.LENGTH_SHORT).show();
}

else
{

adapter.players= (ArrayList<Player>) results.values; 
adapter.notifyDataSetChanged();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM