-1

I need my RecyclerView Image can be pressed and go to next activity. I have the Adapter code.

public class MakananAdapter extends RecyclerView.Adapter<MakananAdapter.ViewHolder> {
ArrayList<Makanan> makananList;

public MakananAdapter(ArrayList<Makanan> makananList)
{
    this.makananList = makananList;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list,parent,false);
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Makanan tanaman = makananList.get(position);
    holder.tvJudul.setText(tanaman.judul);
    /*holder.tvHarga.setText(tanaman.harga);*/
    holder.ivFoto.setImageDrawable(tanaman.foto);
    holder.tvJudul.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
}



@Override
public int getItemCount() {
    if(makananList!=null)
        return makananList.size();
    return 0;
}

public class ViewHolder extends RecyclerView.ViewHolder {
    ImageView ivFoto;
    TextView tvJudul;
    TextView tvHarga;

    public ViewHolder(View itemView) {
        super(itemView);
        ivFoto = (ImageView) itemView.findViewById(R.id.imageView);
        tvJudul = (TextView) itemView.findViewById(R.id.textViewJudul);
        /*tvHarga = (TextView) itemView.findViewById(R.id.textViewHarga);*/
    }
}}

How to Intent RecyclerView in AdapterClass?

Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Aldo Alfalus
  • 13
  • 2
  • 4

1 Answers1

1

Make your constructor like this

private Context context;
public MakananAdapter(ArrayList<Makanan> makananList, Context context) {
    this.makananList = makananList;
    this.context = context;
}

Pass the context when you initialize the adapter

MakananAdapter = new MakananAdapter(makananList, this);

And in adapter in your onClickListner do like this

holder.tvJudul.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {

        Intent openNextActivity = new Intent(context, yourActivityName.class);
        context.startActivity(openNextActivity)
     }
});
AbhayBohra
  • 2,047
  • 24
  • 36