I've been trying to find the answer to my question above but no post seems to help. I have a RecyclerView Adapter where I get the data as List from a database:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> {
public List<Note> noteList;
private View.OnLongClickListener longClickListener;
private Context context;
public RecyclerViewAdapter(List<Note> noteList, View.OnLongClickListener longClickListener, Context context) {
this.noteList = noteList;
this.longClickListener = longClickListener;
this.context = context;
}
And a ViewHolder where I have an onClickListener to find the position of the view clicked using getLayoutPosition()
:
static class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView noteTitleText;
private TextView noteDescriptionText;
private TextView dateText;
private TextView weekText;
RecyclerViewHolder(View view) {
super(view);
noteTitleText = (TextView) view.findViewById(R.id.noteTitleText);
noteDescriptionText = (TextView) view.findViewById(R.id.noteDescriptionText);
dateText = (TextView) view.findViewById(R.id.dateText);
weekText = (TextView) view.findViewById(R.id.weekText);
view.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int position = getLayoutPosition();
// I want the noteList from the RecyclerViewAdapter here.
Note note = noteList.get(position);
Log.d("Item Clicked", String.valueOf(position));
}
}
The purpose is that I need the noteList with all the notes from the RecyclerViewAdapter in the onClick() method to populate an Activity using an intent. How should I go about this?
After trying out solutions for an hour, here's what I settled on. Instead of implementing OnClickListener in ViewHolder, I implemented it in RecyclerViewAdapter in onBindViewHolder() method, as:
@Override
public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {
final Note note = noteList.get(position);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// The note and noteList can be accessed here.
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.