Answer the question
In order to leave comments, you need to log in
How to determine which element of the list is tied to a specific holder?
I have an adapter and a holder for a list. When you click on an element of the list, you need to open a new fragment with a detailed display of this element. How can I determine which garment is tied to a specific holder in order to send the garment.getID data to open the clicked component?
public class GarmentListAdapter extends RecyclerView.Adapter {
private List mGarments = new ArrayList<>();
@NonNull
@Override
public GarmentHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_garment, parent, false);
return new GarmentHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull GarmentHolder holder, int position) {
Garment currentGarment = mGarments.get(position);
holder.mTitleTextView.setText(currentGarment.getTitle());
DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.getDefault());
String dateText = dateFormat.format(currentGarment.getDate());
holder.mDateTextView.setText(dateText);
}
@Override
public int getItemCount() {
if (mGarments != null)
return mGarments.size();
else return 0;
}
public void setGarments(List garments) {
mGarments = garments;
notifyDataSetChanged();
}
class GarmentHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView mTitleTextView;
private TextView mDateTextView;
public GarmentHolder(@NonNull View itemView) {
super(itemView);
itemView.setOnClickListener(this);
mTitleTextView = (TextView) itemView.findViewById(R.id.title_text_view);
mDateTextView = (TextView) itemView.findViewById(R.id.date_text_view);
}
@Override
public void onClick(View v) {
replaceFragment(GarmentFragment.newInstance( garment.getId() ));
}
}
Answer the question
In order to leave comments, you need to log in
In onBindViewHolder, store the element in the holder (or its position, but the element is more convenient).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question