-1

I'm a beginner to Android app development, and I want to know who passes the arguments for the overridden methods when creating a custom Recycler Adapter.

package com.example.cardviewapp;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;

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

    public Context context;
    public ArrayList<HarryPotterNovel> harryPotterNovel;

    public CustomAdapter(Context context, ArrayList<HarryPotterNovel> harryPotterNovel) {
        this.context = context;
        this.harryPotterNovel = harryPotterNovel;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView textView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            this.imageView = itemView.findViewById(R.id.imageView);
            this.textView = itemView.findViewById(R.id.textView);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.card_view_items, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        HarryPotterNovel harryPotterNovel2 = harryPotterNovel.get(position);
        holder.textView.setText(harryPotterNovel2.getTitle());
        holder.imageView.setImageResource(harryPotterNovel2.getImage());
    }

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

This is my CustomAdapter.java code. Who is going to pass these arguments? That is the arguments for the ViewHolder constructor, the onCreateViewHolder() and onBindViewHolder. And secondly why is the context variable created?

Mark Rotteveel
  • 90,369
  • 161
  • 124
  • 175
  • Please don't post what is basically [the same question](https://stackoverflow.com/questions/72412425/please-what-is-the-use-of-creating-a-context-object-in-a-custom-recyclerview-ada) multiple times – Tyler V May 28 '22 at 03:54

0 Answers0