我为项目列表构建了一个Recyclarview,并希望在适配器中标识它们,因此我创建了一个适配器类并重写onCreateViewHolder方法来填充数据,当我通过ViewGroup类从上下文类中创建一个名为context的对象时,通过以下行调用getContext方法:
Context context = ViewGroup.getContext();
但是无法从静态上下文中引用方法getContext。
我尝试使用与getContext相等的getApplicationContext方法,但无法解决,我尝试打开ViewGroup类,但找不到getContext方法
/**
*
* This gets called when each new ViewHolder is created. This happens when the RecyclerView
* is laid out. Enough ViewHolders will be created to fill the screen and allow for scrolling.
*
* @param viewGroup The ViewGroup that these ViewHolders are contained within.
* @param viewType If your RecyclerView has more than one type of item (which ours doesn't) you
* can use this viewType integer to provide a different layout. See
* {@link android.support.v7.widget.RecyclerView.Adapter#getItemViewType(int)}
* for more details.
* @return A new NumberViewHolder that holds the View for each list item
*/
@NonNull
@Override
public NumberViewHolde onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = ViewGroup.getContext();
int layoutIdForListItem = R.layout.number_list_item;
LayoutInflater inflater = LayoutInflater.from(context);
boolean ShouldAttachToParentImmediately = false;
View view =inflater.inflate(layoutIdForListItem,ViewGroup,ShouldAttachToParentImmediately);
NumberViewHolde viewHolde = new NumberViewHolde(view);
return viewHolde;
}
getContext方法getContext不能从静态上下文中引用