i am working on recycler. this is my adapter class:
public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.ViewHolder> {
private static List<Notes> mNotes;
private Context mContext;
public NotesAdapter(Context context, List<Notes> notes) {
mNotes = notes;
mContext = context;
}
// Usually involves inflating a layout from XML and returning the holder
@Override
public NotesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View notesView = inflater.inflate(R.layout.items_notes, parent, false);
// Return a new holder instance
final ViewHolder viewHolder = new ViewHolder(notesView);
notesView.setLongClickable(true);
notesView.setClickable(true);
return viewHolder;
}
// Easy access to the context object in the recyclerview
private Context getContext() {
return mContext;
}
@Override
public void onBindViewHolder(NotesAdapter.ViewHolder viewHolder, final int position) {
// Get the data model based on position
Notes notes = mNotes.get(position);
viewHolder.itemView.setSelected(mNotes.contains(position));
// Set item views based on your views and data model
TextView textView = viewHolder.preTitle;
textView.setText(notes.getTitle());
TextView textView1 = viewHolder.preText;
textView1.setText(notes.getText());
String color=notes.getColor();
CardView preCard=viewHolder.preCard;
preCard.setBackgroundColor(Color.parseColor(color));
ImageView img = viewHolder.preImage;
img.setVisibility(View.GONE);
}
// Returns the total count of items in the list
@Override
public int getItemCount() {
return mNotes.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public RobotoTextView preTitle, preText;
public ImageView preImage;
public CardView preCard;
public MenuItem delete;
public ViewHolder(final View itemView) {
super(itemView);
itemView.setClickable(true);
preTitle = (RobotoTextView) itemView.findViewById(R.id.preTitle);
preText = (RobotoTextView) itemView.findViewById(R.id.preText);
preImage=(ImageView) itemView.findViewById(R.id.preImage);
preCard=(CardView) itemView.findViewById(R.id.preCard);
delete=(MenuItem) itemView.findViewById(R.id.delete);
itemView.setOnLongClickListener(new View.OnLongClickListener() {
int selected=0;
@Override
public boolean onLongClick(View view) {
int p=getLayoutPosition();
System.out.println("LongClick: "+p);
return true;
}
});
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int p=getLayoutPosition();
Notes notes = mNotes.get(p);
Intent intent = new Intent(view.getContext(),EditNote.class);
Bundle bundle = new Bundle();
bundle.putSerializable("DATA",notes);
intent.putExtras(bundle);
itemView.getContext().startActivity(intent);
// Toast.makeText(getContext(), "Recycle Click" + mNotes.get(viewHolder.getPosition())+" ", Toast.LENGTH_SHORT).show();
System.out.println("Click: "+p);
}
});
}
}
}
here, click listener is working fine. and long click also. i am able to get clicked and long clicked item positions. but now i want to access menu items from this class. in long click listener method. i want to write code, to access menu item, the id of that item is R.id.delete.
on long click i want to set that item visible. but.
delete.setVisible(true);
gives me null object reference error. any idea? or suggestions?