【问题标题】:Android Recycler View problem in "onBindViewHolder" function "static method cannot be called from non-static method"“onBindViewHolder”函数中的Android Recycler View问题“无法从非静态方法调用静态方法”
【发布时间】:2020-01-17 19:59:11
【问题描述】:

我是 android 和 java 的新手。我无法在onBindViewHolder 函数中调用ViewHolder.setCategoryName(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) {
        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);
        }
    }
}

【问题讨论】:

  • 尝试holder.setCategoryName(...) 而不是ViewHolder.setCategoryName(name); BTW 类名后跟. 后跟方法名被解释为调用Java 中的静态方法
  • 尝试使用 public void setCategoryName(String name){ categoryName.setText(name); }

标签: java android android-recyclerview static non-static


【解决方案1】:

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

 private void setCategoryName

你需要这样做:

 private static void setCategoryName

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

holder.bind(categoryModelList.get(position))

【讨论】:

    【解决方案2】:

    试试这个:

    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());
            }
        }
    }
    

    【讨论】:

    • 这对我帮助很大,谢谢大家!这个社区太棒了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多