gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:38:09 33 4
gpt4 key购买 nike

我想在单击 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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com