在getContext()
中,其显示“无法从静态上下文中引用非静态方法'getContext()'”如何解决该问题,或者应代替该调用什么。
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder > {
private List<CategoryModel> CategoryModelList;
public CategoryAdapter(List<CategoryModel> categoryModelList) {
CategoryModelList = categoryModelList;
}
@NonNull
@Override
public CategoryAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(ViewGroup.getContext()).Inflate(R.layout.category_item,viewGroup,false);
return null;
}
@Override
public void onBindViewHolder(@NonNull CategoryAdapter.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 0;
}
答案 0 :(得分:3)
ViewGroup.getContext()
尝试:
viewGroup.getContext()
答案 1 :(得分:2)
您正在使用ViewGroup
,但是应该是viewGroup
答案 2 :(得分:0)
尝试:
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder > {
private Context context;
private List<CategoryModel> CategoryModelList;
public CategoryAdapter(List<CategoryModel> categoryModelList, Context Mcontext) {
CategoryModelList = categoryModelList;
this.context = Mcontext;
}
@NonNull
@Override
public CategoryAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(context).Inflate(R.layout.category_item,viewGroup,false);
return null;
}
@Override
public void onBindViewHolder(@NonNull CategoryAdapter.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 0;
}