AdInfinite9325
AdInfinite9325

Reputation: 13

View.OnClickListener not switching to another activity

Been trying to create a food delivery application as a project for colleege and the View.OnClickListener is greyed out in Android Studio and as a result, the switch between activities doesn't seem to work at all.

Excerpt from MainAdapter.java

...

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.viewholder> {

    ArrayList<MainModel> list ;
    Context context;

    public MainAdapter(ArrayList<MainModel> list, Context context) {
        this.list = list;
        this.context = context;
    }

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

    @Override
    public void onBindViewHolder(@NonNull viewholder holder, int position) {

        final MainModel model = list.get(position);
        holder.foodimage.setImageResource(model.getImage());
        holder.mainName.setText(model.getName());
        holder.price.setText(model.getPrice());
        holder.description.setText(model.getDescription());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, DetailActivity.class);
                intent.putExtra("image", model.getImage());
                intent.putExtra("price", model.getPrice());
                intent.putExtra("desc", model.getDescription());
                intent.putExtra("name", model.getName());
                context.startActivity(intent);
            }
        });
        });
    }

Here is the complete MainAdapter.java file: https://pastebin.com/F7TWa3nf

Upvotes: 0

Views: 358

Answers (1)

Wahdat Jan
Wahdat Jan

Reputation: 4156

You are calling holder.itemView but itemView is not defined in your viewholder class .

You can use

holder.foodimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, DetailActivity.class);
                intent.putExtra("image", model.getImage());
                intent.putExtra("price", model.getPrice());
                intent.putExtra("desc", model.getDescription());
                intent.putExtra("name", model.getName());
                context.startActivity(intent);
            }
        });

Upvotes: 1

Related Questions