I have a recycler view that I want to toast the id of the clicked item. Instead every time I toast I get the first item regardless of the item that I toast.
It toasts paracetamol regardless of whether i toast quinine or brufen or whichever.
Here is my code:
public class DrugsAdapter extends RecyclerView.Adapter<DrugsAdapter.ViewHolder> {
private List<DrugModel> drugList;
private Context mContext;
private RecyclerView mRecyclerV;
DrugModel drugModel;
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView drugName,drugMode;
public View layout;
public ViewHolder(View v) {
super(v);
layout = v;
drugName = (TextView) v.findViewById(R.id.drugName);
drugMode = v.findViewById(R.id.drugModeOfAction);
drugName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
}
public void add(int position, DrugModel drug) {
drugList.add(position, drug);
notifyItemInserted(position);
}
public void remove(int position) {
drugList.remove(position);
notifyItemRemoved(position);
}
// Provide a suitable constructor (depends on the kind of dataset)
public DrugsAdapter(List<DrugModel> myDataset, Context context, RecyclerView recyclerView) {
drugList = myDataset;
mContext = context;
mRecyclerV = recyclerView;
}
// Create new views (invoked by the layout manager)
@Override
public DrugsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
// drugModel = new DrugModel();
// drugModel = drugList.get(viewType);
// create a new view
LayoutInflater inflater = LayoutInflater.from(
parent.getContext());
View v =
inflater.inflate(R.layout.drug_item, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String a = drugModel.getDrugId();
Toast.makeText(mContext,"id "+a,Toast.LENGTH_LONG).show();
}
});
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final DrugModel drug = drugList.get(position);
holder.drugName.setText(drug.getdrugName());
holder.drugMode.setText(drug.getMode());
}
@Override
public int getItemCount() {
return drugList.size();
}
}
I intend to pass the toasted parameter item to the next activity as an intent but that now is a problem.