2017-07-12 45 views
0

我想打印祝酒时,我点击一个recyclerview,我在OnClick方法中的变量“上下文”中有错误,我没有找到如何解决这个错误。添加上下文到Toast.MakeText

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

的getContext()? – PedroHawk

+0

你可以使用'view.getContext()' –

+0

非常感谢大家! –

回答

1

更改它如下

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