Android:Toast类型中的方法makeText(Context,CharSequence,int)不适用于参数(ListViewAdapter,String,int)

时间:2016-05-06 11:12:55

标签: java android listview

我正在使用过滤器类中的toast。这是它的代码

    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        expressionlist.clear();
        if (charText.length() == 0) {
            expressionlist.addAll(arraylist);
        } else {
            for (Expression wp : arraylist) {
                if (wp.getWord().toLowerCase(Locale.getDefault())
                        .contains(charText)) {
                    expressionlist.add(wp);
                }
            }
            if(expressionlist.size() == 0){
                Toast toast = Toast.makeText(this, "No match found", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                toast.show();
                   // add all items of expressionlist
                expressionlist.addAll(arraylist);
                }

        }

        notifyDataSetChanged();
    }
}

我收到错误Toast类型中的方法makeText(Context, CharSequence, int)不适用于参数(ListViewAdapter, String, int) 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

您应该引用上下文,即getApplicationContext()或传递ListViewAdapter的上下文。

上下文可以是ActivityFragment

Toast toast = Toast.makeText(context, "No match found", Toast.LENGTH_LONG);