I have a custom object :
Student.class
public class Student {
private String name;
private String age;
public String getName() {
return name;
Simplest way is holding single ItemClickListener
inside your Adapter:
public class WalletListRecyclerAdapter extends RecyclerView.Adapter {
private List wallets;
private ItemClickListener itemClickListener;
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Wallet wallet = wallets.get(position);
holder.root.setOnClickListener(v -> {
if (itemClickListener != null)
itemClickListener.onClick(holder.root, wallet);
});
}
static class ViewHolder extends RecyclerView.ViewHolder {
View root;
public ViewHolder(View v) {
super(v);
root = v;
}
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
public interface ItemClickListener {
void onClick(View view, Wallet wallet);
}
}
In Activity:
walletsAdapter = new WalletListRecyclerAdapter();
walletsAdapter.setItemClickListener((view, wallet) -> {
Intent intent = new Intent(this, WalletActivity.class);
intent.putExtra(Const.KEY_WALLET, wallet);
startActivity(intent);
});