1

I am new to android and java. I am not able to call ViewHolder.setCategoryName(name); in the onBindViewHolder function. I know there are probably similar questions but nothing has worked for me yet. The compiler gives error "non-static functions cannot be called from a static context", I haven't used static keyword anywhere in my code.

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);
        }
    }
}
aksh1618
  • 2,245
  • 18
  • 37
  • 2
    try `holder.setCategoryName(...)` instead of `ViewHolder.setCategoryName(name);` BTW Class name followed by `.` followed by method name is interpreted as calling a static method in Java – Bö macht Blau Jan 17 '20 at 20:07
  • try with public void setCategoryName(String name){ categoryName.setText(name); } – Kishan Maurya Jan 17 '20 at 20:16
  • Does this answer your question? [calling non-static method in static method in Java](https://stackoverflow.com/questions/2042813/calling-non-static-method-in-static-method-in-java) – Ryan M Jan 18 '20 at 00:57

2 Answers2

1

The problem is that your method isn't static but you are trying to call from a static context:

 private void setCategoryName

You would need to do:

 private static void setCategoryName

However, for this type of action, you can just use the holder variable:

holder.bind(categoryModelList.get(position))
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
0

try this:

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());
        }
    }
}
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156