java - 将上下文添加到 Toast.MakeText

标签 java android toast

我想在单击 recyclerview 时打印 toast ,我在 OnClick 方法中的变量“context”中出现错误,我不知道如何修复此错误。

private class MyOnClickListener implements View.OnClickListener{

    @Override
    public void onClick(View view) {


        int itemPosition = recyclerView.getChildLayoutPosition(view);
        String item = items.get(itemPosition);
        Toast.makeText(context, item, Toast.LENGTH_LONG).show();
    }
}

@Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // get inflater and get view by resource id itemLayout
        View v = LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false);
        // return ViewHolder with View
        v.setOnClickListener(mOnClickListener);

        return new ViewHolder(v);
    }


    /**
     * Get the size of items in adapter
     * @return the size of items in adapter
     */
    @Override
    public int getItemCount() {
        return items.size();
    }
    /**
     * Bind View Holder with Items
     * @param holder: the view holder
     * @param position : the current position
     */

最佳答案

修改如下

Toast.makeText(view.getContext(), item, Toast.LENGTH_LONG).show();

关于java - 将上下文添加到 Toast.MakeText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45062738/

相关文章:

java - Android 检索手机联系人查询需要很长时间

jsp - Struts2 和 jsp 表单使用 jquery ajax post 提交并得到类似 toast 的消息,该消息来了又消失?

java - 为什么 child 会被夹在定制的 toast 里?

javascript - 我需要在 js 中创建我的 CSS 类的多个实例

java - 正确的 Info.plist 设置以在特定 JVM 中启动 Java 应用程序

java - 在 Eclipse 插件中搜索项目文件

java - (java) 确保插件工厂存在特定的构造函数

android - 对内容提供者强制执行项目所有权权限

android - 格式错误的 android list 违反条款

android - xamarin android 构建错误 : "Failed to create JavaTypeInfo for class"