我想在点击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
*/
答案 0 :(得分:1)
按如下方式更改
Toast.makeText(view.getContext(), item, Toast.LENGTH_LONG).show();