3 回答

TA贡献1859条经验 获得超6个赞
所有告诉您将上下文作为参数的答案都是错误的或多余的,您已经有一种从视图中获取上下文的方法:
@Override
public void onBindViewHolder(@NonNull GameViewHolder holder, int position) {
Context context = holder.itemView.getContext();
}

TA贡献1780条经验 获得超4个赞
在您的适配器构造函数中,添加上下文作为参数。
public Context context;
public YourAdapterClass(Context context){
this.context = context;
}
@Override
public void onBindViewHolder(@NonNull GameViewHolder holder, int position) {
holder.mTextPar.setBackground(ContextCompat.getDrawable(context, R.drawable.border_box));
}

TA贡献1906条经验 获得超3个赞
不清楚你说的是什么适配器,ArrayAdapter
适配器RecyclerView
;请提及;还可以编写更多代码来帮助其他人了解需要上下文的内容/位置。通常,如果它是RecyclerView
Adapter,则视图的上下文将会,例如itemView.getContext()
,如果是ArrayAdapter
,则有一个getContext()
方法。
添加回答
举报