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()
方法。
添加回答
舉報