Android "onBindViewHolder" 函数中的 Recycler 查看问题 "static method cannot be called from non-static method"

Android Recycler View problem in "onBindViewHolder" function "static method cannot be called from non-static method"

我是 android 和 java 的新手。我无法在 onBindViewHolder 函数中调用 ViewHolder.setCategoryName(name);。我知道可能有类似的问题,但还没有对我有用。编译器给出错误 "non-static functions cannot be called from a static context",我没有在代码中的任何地方使用 static 关键字。

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {

    private List<CategoryModel> categoryModelList;

    public CategoryAdapter(List<CategoryModel> categoryModelList) {
        this.categoryModelList = categoryModelList;
    }

    @NonNull
    @Override
    public CategoryAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.category_item,viewGroup,false);
        return new ViewHolder(view);
    }

    @Override
    public  void onBindViewHolder(@NonNull CategoryAdapter.ViewHolder holder, int position) {
        String icon = categoryModelList.get(position).getCategoryIconLink();
        String name = categoryModelList.get(position).getCategoryName();
        ViewHolder.setCategoryName(name);

    }


    @Override
    public int getItemCount() {
        return categoryModelList.size();
    }


    public class ViewHolder extends  RecyclerView.ViewHolder{

       private ImageView categoryIcon;
       private TextView categoryName;

        public ViewHolder(@NonNull View itemView)  {
              super(itemView);
              categoryIcon = itemView.findViewById(R.id.category_icon);
              categoryName = itemView.findViewById(R.id.category_name);

        }

        private void setCategoryIcon(){

        }
        private void setCategoryName(String name){

            categoryName.setText(name);
        }
    }
}

试试这个:

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {

    private List<CategoryModel> categoryModelList;

    public CategoryAdapter(List<CategoryModel> categoryModelList) {
        this.categoryModelList = categoryModelList;
    }

    @NonNull
    @Override
    public CategoryAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.category_item,viewGroup,false);
        return new ViewHolder(view);
    }

    @Override
    public  void onBindViewHolder(@NonNull CategoryAdapter.ViewHolder holder, int position) {

    holder.bind(categoryModelList.get(position))

    }


    @Override
    public int getItemCount() {
        return categoryModelList.size();
    }


    public class ViewHolder extends  RecyclerView.ViewHolder{

       private ImageView categoryIcon;
       private TextView categoryName;

        public ViewHolder(@NonNull View itemView)  {
              super(itemView);
              categoryIcon = itemView.findViewById(R.id.category_icon);
              categoryName = itemView.findViewById(R.id.category_name);

        }

       public void bind(CategoryModel categoryModel){

             categoryName.setText(categoryModel.getCategoryName());
        }
    }
}

问题是您的方法不是静态的,但您正试图从静态上下文中调用:

 private void setCategoryName

您需要做:

 private static void setCategoryName

但是,对于这种类型的操作,您可以只使用 holder 变量:

holder.bind(categoryModelList.get(position))